在 Javascript 中使用空格分割字符串?
我需要一个分词器,给定单词之间具有任意空格的字符串将创建一个没有空子字符串的单词数组。
例如,给定一个字符串:
" I dont know what you mean by glory Alice said."
我使用:
str2.split(" ")
这也会返回空子字符串:
["", "I", "dont", "know", "what", "you", "mean", "by", "glory", "", "Alice", "said."]
如何从数组中过滤掉空字符串?
I need a tokenizer that given a string with arbitrary white-space among words will create an array of words without empty sub-strings.
For example, given a string:
" I dont know what you mean by glory Alice said."
I use:
str2.split(" ")
This also returns empty sub-strings:
["", "I", "dont", "know", "what", "you", "mean", "by", "glory", "", "Alice", "said."]
How to filter out empty strings from an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能甚至不需要过滤,只需使用以下正则表达式进行拆分:
You probably don't even need to filter, just split using this Regular Expression:
返回非空格序列列表
["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said ."]
(请注意,这包括“said.”中的点。)返回所有单词的列表:
["I", "dont", "know", "what", "you" 、“意味着”、“通过”、 "glory", "Alice", "said"]
匹配()
returns a list of non-space sequences
["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said."]
(note that this includes the dot in "said.")returns a list of all words:
["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said"]
docs on match()
在使用 split 之前应该修剪字符串。
You should trim the string before using split.
我推荐
.match
:这会匹配单词边界之间的单词,因此所有空格都不匹配,因此不会包含在结果数组中。
I recommend
.match
:This matches words between word boundaries, so all spaces are not matched and thus not included in the resulting array.
请参阅
filter
方法http://www.hunlock.com/blogs/ Mastering_Javascript_Arrays#quickIDX13
see the
filter
methodhttp://www.hunlock.com/blogs/Mastering_Javascript_Arrays#quickIDX13
我认为出现空子字符串是因为有多个空格,您可以在 for 循环中使用 Replace() 将多个空格替换为单个空格,然后使用 split() 使用单个空格拆分程序像这样:
i think empty sub-string happen because there are multiple white-spaces you can use a replace() in a for loop to replace multiple white-spaces with a single white-space then split() to split the program using a single white space like this:
这就是我们所需要的:
That is all that we need: