Jump to content

Split() not working


thecard

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.