我有一个字符串String test = "-b--b--";
我想要一个包含 {"", "b", "", "b", ""} 的数组
如何调用string.split()
函数来获取它?
编辑:
我尝试过test.split("-")
。
我的测试给出了 {"", "b", "", "b"}
。
如果该方法确实包含第一对连字符之间的第一个空格,为什么它不包含数组上的最后一个空格?
基本上我想获取连字符之间的所有字符,其中 String test = ---
应该给我 {"", "", ""}
最佳答案
它在 docs 中提到过
<小时/>Trailing empty strings not included in the resulting array.
为了得到想要的结果,将其更改为
test.split("-",Integer.MAX_VALUE);
这将考虑空字符串,因此输出将是
"", "b", "", "b", "" , ""
关于java - string.split() 和处理重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18815911/