开发者

Java string split function acting strange

开发者 https://www.devze.com 2022-12-16 12:37 出处:网络
I am noticing strange behaviour when using the split() method in Java. I have a string as follows: 0|1|2|3|4|5|6|7|8|9|10

I am noticing strange behaviour when using the split() method in Java.

I have a string as follows: 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]);
}

This will produce the desired results:

Length: 11
0
1
2
3
4
5
6
7
8
9
10

However if I receive the string开发者_Python百科: 0|1|2|3|4|5|6|7|8||

I get the following results:

Length: 8
0
1
2
3
4
5
6
7
8

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 returns the entire string with a length of 1.

Any help would be greatly appreciated!


The default behavior of split is to not return empty tokens (because of a zero limit). Use the two parameter split method with a limit of -1 will give you all empty tokens in the return.

UPDATE:

Test code as follows:

public class Test {
    public static void main(String[] args) {
    String currentString[] = "0|1|2|3|4|5|6|7|8||".split("\\|", -1);
    System.out.println("Length:"+currentString.length); 
    for(int i=0;i < currentString.length;i++){ System.out.println(currentString[i]); }
  }
}

Output as follows:

Length:11
0
1
2
3
4
5
6
7
8
--- BLANK LINE --    
--- BLANK LINE --

The "--- BLANK LINE --" is put in by me to show that the return is blank. It is blank once for the empty token after 8| and once for the empty trailing token after the last |.

Hope this clears things up.


String.split() is weird.

Its extreme weirdness, in this and other ways, are some of the reasons why we made Splitter.

It has less surprising behavior and lots of flexibility.


My Java is a little bit rusty, but shouldn't it be:

String currentString[] = "0|1|2|3|4|5|6|7|8||".split("\\|");
System.out.println("Length:"+currentString.length); 
for(int i = 0; i < currentString.length; i++)
{
  System.out.println(currentString[i]); 
} 


IMO, I think this is the default behavior of split, Anyway please try this:

String currentString[] = br.readLine().replace("||","| |").split("\|"); System.out.println("Length:"+currentString.length); for(int i=0;i < currentString.length;i++){ System.out.println(currentString[i]); }

This has not been tested yet, but i think this should do the trick.


You need to use indexOf() and then substring() for this to work. I don't think you can empty string by using split() only.


Please check the following code, I used your solution, it works:


public class SplitTest 
{
    public static void main(String[] args)
    {
      String text = "0|1|2|3|4|5|6|7|8||";
      String pattern = "\\|";
      String [] array = text.split(pattern, -1);
          System.out.println("array length:" + array.length);
          for(int i=0; i< array.length; i++) 
          System.out.print(array[i]+ " ");
        } 
 }

output is:

array length:11
0 1 2 3 4 5 6 7 8   
0

精彩评论

暂无评论...
验证码 换一张
取 消