我split()在Java中使用该方法时注意到奇怪的行为.
我有一个字符串如下: 0|1|2|3|4|5|6|7|8|9|10
String currentString[] = br.readLine().split("\\|");
System.out.println("Length:"+currentString.length);
for(int i=0;i < currentString.length;i++){
System.out.println(currentString[i]);
}
Run Code Online (Sandbox Code Playgroud)
这将产生预期的结果:
Length: 11
0
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud)
但是,如果我收到字符串: 0|1|2|3|4|5|6|7|8||
我得到以下结果:
Length: 8
0
1
2
3
4
5
6
7
8
Run Code Online (Sandbox Code Playgroud)
The final 2 empties are omitted. I need the empties to be kept. Not sure what i am doing wrong. I have also tried using the split in this manner as well. ...split("\\|",-1);
but that …