我的输入字符串是
String s = ",1,,2, ,3,4,5,,6";
String[] array = s.split(",");
System.out.println(Arrays.toString(array));
现在我得到这个输出
[, 1, , 2, , 3, 4, 5, , 6]
但预期的输出是
[1, 2, 3, 4, 5, 6]
最佳答案
使用这个增强的正则表达式来避免空匹配:
s.replaceFirst("^( *, *)+", "").split("(, *)+");
//=> [1, 2, 3, 4, 5, 6]
关于java - 如何从数组中删除空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997941/