奇怪的 String.split(" ") 行为 [英] Strange String.split(" ") behavior
问题描述
我有一个 .txt 文本文件,其中包含一些行..我使用 RequestBuilder 对象加载包含,并将 responseText 与words = String.split(" ");但我想知道,为什么结果包含 "部分..例如,我的文字:
I have a .txt text file, containing some lines.. I load the contain using the RequestBuilder object, and split the responseText with words = String.split(" "); but i wonder, why the result is contains the " " part.. For example, my text:
abc
def
ghi
结果是,
words[0] = "abc
"
words[1] = "def
"
words[2] = "ghi
"
非常感谢任何帮助.提前致谢.
Any help is highly appreciated. Thanks in advance.
推荐答案
Windows 回车符 ("
"
) 不应该对您的结果产生明显的影响,您也不应该需要转义您传递给 String.split()
的正则表达式.
Windows carriage returns ("
"
) shouldn't make a visible difference to your results, nor should you need to escape the regular expression you pass to String.split()
.
以下是使用 str.split("
")
对上述两种情况的证明:http://ideone.com/4PnZi
Here's proof of both the above using str.split("
")
: http://ideone.com/4PnZi
如果你确实有 Windows 回车,你应该(尽管绝对没有必要)使用 str.split("
")
: http://ideone.com/XcF3C
If you do have Windows carriage returns though, you should (although strictly not necessary) use str.split("
")
: http://ideone.com/XcF3C
这篇关于奇怪的 String.split(" ") 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!