thecard Posted December 21, 2009 Share Posted December 21, 2009 I have a string called stringexample which I want to split up into an array containing the individual words in the original string. When I try to split it up however it doesn't split as I would like: when the alert comes up, the string has been split as following: [0] = "DATE:", [1] =" 2009/05/03", [2] = "", [3] =" Hello: WORLD". For some reason "Hello: WORLD" isn't split up into 2 strings. Why not? function aftersplit() { var splitres = Array(); stringexample = "DATE: 2009/05/03 Hello: WORLD" stringexample = stringexample.split(" "); for(i = 0; i < stringexample.length; i++){ alert(stringexample); } } Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/185951-split-not-working/ Share on other sites More sharing options...
salathe Posted December 22, 2009 Share Posted December 22, 2009 There not only space whitespace characters in that string; there are also tab characters. Substituting space for [sPC] and tab for [TAB] the string is DATE:[sPC][TAB]2009/05/03[sPC][sPC][TAB]Hello:[TAB]WORLD (according to what I could copy/paste from your post). If you want to split on any whitespace, use a regular expression like /\s+/ in split. Link to comment https://forums.phpfreaks.com/topic/185951-split-not-working/#findComment-981940 Share on other sites More sharing options...
thecard Posted December 22, 2009 Author Share Posted December 22, 2009 Thanks a lot Makes sense! Link to comment https://forums.phpfreaks.com/topic/185951-split-not-working/#findComment-981946 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.