Home > OS >  Java, splitting string into array
Java, splitting string into array

Time:02-20

I am trying to split a string into string array. And I have stumbled to something strange to me. I don't understand why it works like this.

   String one, two;
    
    one = "";
    two = ":";
    
    
    String[] devided1 = one.trim().split(":");
    String[] devided2 = two.trim().split(":");
    
    System.out.println("size: "  devided1.length);
    System.out.println("size: "  devided2.length);
    

I get output:

   size: 1
   size: 0

Why is empty string giving me size of one, but string that only has the delimiter gives my array size of 0? I saw more confusing things like: that size of "::" is 0, but size of ": :" is 2, not 3. Can someone please explain it to me?

CodePudding user response:

See the doc comment in source code or documentation for public String[] split(String regex, int limit) method.

Case 1:

String one = "";
String[] devided1 = one.trim().split(":");

The resulting array will have 1 element = original string String[1] [""], because expresion ":" was not match any part of the input string.

According to documentation:

If the * expression does not match any part of the input then the resulting array * has just one element, namely this string.

Case 2:

String two = ":";
String[] devided2 = two.trim().split(":");

The split(":") has default limit = 0. It means that from the resulting array trailing empty strings will be removed. So method splits ":" string to array with two empty strings and then remove them and as result we get empty array.

According to documentation:

If limit is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Case 3:

String two = ":";
String[] devided2 = two.trim().split(":", -1);

We will get an array with two empty strings.

According to documentation:

If limit is non-positive then the pattern will be applied as many times as possible and the array can have any length

Case 4:

String two = "::";
String[] devided2 = two.trim().split(":");

We will get empty array. It is the same like Case 2.

Case 5:

String one = ": :";
String[] devided1 = one.trim().split(":");

The method will split string to three array elements ["", " ", ""] and then remove empty strings from the end of array, because limit = 0. We will get String[2] ["", " "].

According to documentation:

If limit is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

CodePudding user response:

This link is helpful:

https://konigsberg.blogspot.com/2009/11/final-thoughts-java-puzzler-splitting.html

Basically, it is for perl compatibility.

You can use split(":", -1) here if you don't want that behavior.

Otherwise, split(":") defaults to split(":", 0), and the difference is:

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#split(java.lang.String,int)

  • If the limit is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
  • If the limit is negative then the pattern will be applied as many times as possible and the array can have any length.

In case of ":" being splitted, it would result in {"" , ""}, but empty traling spaces will be discarded, so it will return an empty array.

  • Related