views:

63

answers:

3

I have a space separated string that I want to make into an array. I am using the .split(' ') method to do this. Will the resulting array have those spaces in it? For example if my string is "joe walked down the street" and I performed the method on it would the array look like this ["joe", "walked", "down", "the", "street"] or will it look like this ["joe ", "walked ", "down ", "the ", "street "]?

+8  A: 

Nope, it would not have the spaces in there. It would look like this:

["joe", "walked", "down", "the", "street"]

Since spaces are a bit hard to see, let's take a more visible example with the same effect:

var str = "joe...walked...down...the...street";
var arr = str.split("...");
alert(arr); //["joe", "walked", "down", "the", "street"]

You can test it here.

Nick Craver
ok, thanks. Great!
chromedude
+1  A: 

It will remove the spaces.

http://www.w3schools.com/jsref/jsref_split.asp

Robert Harvey
+2  A: 

Note that for more complicated uses of split (eg splitting on a regexp), IE's split does NOT work correctly. There is a cross-browser implementation of split that works correctly.

See http://stackoverflow.com/questions/1453521/javascript-split-doesnt-work-in-ie

Larry K
ah... that would be good to know, thanks
chromedude