0

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 "]?

chromedude
  • 4,246
  • 16
  • 65
  • 96
  • 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 Oct 22 '10 at 01:36
  • @Daniel Mendel ok, thanks. I should have thought of that. – chromedude Oct 22 '10 at 01:38

3 Answers3

9

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
  • 623,446
  • 136
  • 1,297
  • 1,155
2

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 JavaScript: split doesn't work in IE?

Community
  • 1
  • 1
Larry K
  • 47,808
  • 15
  • 87
  • 140
1

It will remove the spaces.

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

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501