当我用多个逗号分割一个字符串时,它们在数组中被忽略。这是对的吗 ?
String a = "a,b,c,,f,,";
String[] sdf = a.split(",");
for (int i = 0; i < sdf.length; i++) {
System.out.println(sdf[i]);
}
或者我错过了什么。
我得到的输出是
[a,b,c, ,f]
我想考虑这些空白值,期待这样的事情
[a,b,c, ,f, ,]
最佳答案
您需要使用:
public String[] split(String regex,
int limit)
并说:
String[] sdf = a.split(",", -1);
对于您的输入,它会产生:
[a, b, c, , f, , ]
引自documentation :
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.
关于java - Split 方法忽略末尾的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20068416/