Jim
Jim

Reputation: 19552

Not sure how string split actually works in this case

I don't get the following:

In the following String:

String s = "1234;x;;y;";

if I do:
String[] s2 = s.split(";");

I get s2.length to be 4 and

s2[0] = "1234";  
s2[1] = "x";  
s2[2] = "";  
s2[3] = "y"; 

But in the string: String s = "1234;x;y;;";

I get:

s2.length to be 3 and

s2[0] = "1234";  
s2[1] = "x";  
s2[2] = "y"; 

?

What is the difference and I don't get 4 in the latter case as well?

UPDATE:
Using -1 is not was I was expecting as behavior.
I mean the last semicolon is the end of the String so in the latter example I was also expecting 4 as length of the array

Upvotes: 16

Views: 1661

Answers (6)

Boris Strandjev
Boris Strandjev

Reputation: 46943

Trailing empty strings are omitted. However, there are ways to include them explicitly, if needed.

Upvotes: 4

Pulkit Goyal
Pulkit Goyal

Reputation: 5654

From the docs,

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

UPDATE:

You have five substrings separated by ; In the second case, these are 1234, x, y, and . As per the docs, all empty substrings (at the end) which result from the split operation would be eliminated. For details, look here.

If n 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.

The string boo:and:foo, for example, yields the following results with these parameters:

Regex   Limit   Result
  :       2     { "boo", "and:foo" }
  :       5     { "boo", "and", "foo" }
  :      -2     { "boo", "and", "foo" }
  o       5     { "b", "", ":and:f", "", "" }
  o      -2     { "b", "", ":and:f", "", "" }
  o       0     { "b", "", ":and:f" }   // all the empty substrings at the end were eliminated

Upvotes: 10

Farm
Farm

Reputation: 3396

Thats default behavior of split method in java to not return empty tokens . ]

s.split("\;", -1); should return empty token

Upvotes: 1

Adel Boutros
Adel Boutros

Reputation: 10285

Why not check what does the documention says first. Here is the link:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

And here is your answer:

Trailing empty strings are therefore not included in the resulting array.

Upvotes: 0

posdef
posdef

Reputation: 6532

Good question. If you check the API documentation for String.split() and check the example with "boo:foo" then you can see that the trailing empty strings are omitted.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The string "boo:and:foo", for example, yields the following results with these expressions:

Regex Result

: { "boo", "and", "foo" }

o { "b", "", ":and:f" }

Upvotes: 2

user647772
user647772

Reputation:

From http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

Upvotes: 3

Related Questions