String.split behavior in java 1.6?

My code is:

String s = "1;;;; 23;;";
System.out.println(s.split(";").length);

and gives as a conclusion 5. Source Code split:

public String[] split(String regex) {
        return split(regex, 0);
    }

and the documentation says:

This method works as if you were calling two split arguments (java.lang.String, int) with the given expression and a limit argument of zero. Therefore, trailing empty lines are not included in the resulting array.

For example, the string "boo: and: foo" gives the following results: with these expressions:

Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }

If I print the lines that I have:

1



 23

Can I get 1;;;; 23;;something like this from this {"1", "", "", "", " 23", ""}?

+2
source share
3 answers

No, five are correct, as indicated in your cited documents:

, .

. , , , -1.

+8

limit = 0, .

System.out.println(s.split(";", -1).length);

7

+2

It will break the string ever ';' be present and placed in an array.

0
source

Source: https://habr.com/ru/post/1547383/


All Articles