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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.