开发者

Would the javascript .split(' ') leave the commas in the array?

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 perfor开发者_如何学Pythonmed 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 "]?


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.


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?


It will remove the spaces.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜