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 "]
?
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 "]
?
- 3 In light of your username... you can test out javascript interactively when using Chrome's built in javascript console (View > Developer > JavaScript Console on mac, Page > Developer > Debug Javascript pc, i think). For questions like this one, it might be faster to just pull up your console and find out. It's also a handy tool when answering questions on StackOverflow ;o – Daniel Mendel Commented Oct 22, 2010 at 1:36
- @Daniel Mendel ok, thanks. I should have thought of that. – chromedude Commented Oct 22, 2010 at 1:38
3 Answers
Reset to default 9Nope, 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.
Note that for more plicated 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 JavaScript: split doesn't work in IE?
It will remove the spaces.
http://www.w3schools./jsref/jsref_split.asp