想象你有:
String string = "A;B;;D";
String[] split = string.split(";");
我想要的结果是:
split[0] = "A";
split[1] = "B";
split[2] = "";
split[3] = "D";
但结果是:
split[0] = "A";
split[1] = "B";
split[2] = "D";
有一个简单的正确方法吗?
最佳答案
使用重载方法split(String regex, int limit)
:
String string = "A;B;;D";
String[] split = string.split(";", -1);
<小时/>
来自documentation :
The string "boo:and:foo", for example, yields the following results with these parameters:
Regex Limit Result
: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "b", "", ":and:f" }
关于java - split() 分隔符之间没有任何内容 = 空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11750202/