在public String\[\] split(String regex)
的java文档中
我们可以找到示例:
The string
"boo:and:foo"
, for example, yields the following results with these expressionsRegex Result
o
{ "b", "", ":and:f" }
第二个结果""
来自哪里?来自 b
和 oo
之间,还是 oo
和 :and:f
之间?
最佳答案
当您使用分隔符o
并且有两个相邻的分隔符时,它会产生一个空字符串。例如,如果您有一个字符串 foo::bar
并将其与 :
分割,您将得到 {"foo", "", "bar"}
因为 ::
之间的空格在分割时被视为空字符串。
如果您想在不包含任何空字符串的情况下进行拆分,请查看 this question .
结束空字符串?
When split is given zero or no limit argument it discards trailing empty fields
引用号:Java: String split(): I want it to include the empty strings at the end
关于java 字符串分割函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119032/