最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览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 "]?

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

Share Improve this question asked Oct 22, 2010 at 1:11 chromedudechromedude 4,30216 gold badges69 silver badges96 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论