Home arrow static arrow Java Programming [Archive] - String.split() behaviour
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - String.split() behaviour
This topic has 2 replies on 1 page.

Posts:10,967
Registered: 4/7/01
String.split() behaviour  
Aug 5, 2004 9:56 PM



 
Given the statement:

String[] s = "122".split("2", -1);

The resultant array is { "1", "", "" }

Next, given the statement:

String[] s = "1223".split("2", -1);

The resultant array is { "1", "", "3" }

Can someone please explain why the second array is not { "1", "", "", "3" }

I'm aware that the API documentation matches the results, however I can't see the logic of it. Why does it split "22" in the first example into two zero-length strings, but doesn't in the second?
 

Posts:1,085
Registered: 4/14/03
Re: String.split() behaviour  
Aug 5, 2004 10:32 PM (reply 1 of 2)



 
I'm aware that the API documentation matches the
results, however I can't see the logic of it. Why does
it split "22" in the first example into two
zero-length strings, but doesn't in the second?

The input is broken up into tokens that are delimited on each side by one of the following:
1) the beginning of the input
2) a subsequence that matches the pattern
3) the end of the input

When you give a negative number as the limit parameter, the split method will return the MAXIMUM number of tokens that it can produce from the input String.

Lets list all of the delimiters in the input:
1) The begging of the input (^)
2) The first occurance of '2' (2-1)
3) The second occurance of '2' (2-2)
4) The end of the input ($)

So, 4 delimiters means 3 tokens. Lets list what the tokens are for each input String your gave an example of:

Example "122"
from ^ to 2-1 gives "1"
from 2-1 to 2-2 gives ""
from 2-2 to $ gives ""

Example "1223"
from ^ to 2-1 gives "1"
from 2-1 to 2-2 gives ""
from 2-2 to $ gives "3"

Does this help at all?
 

Posts:10,967
Registered: 4/7/01
Re: String.split() behaviour  
Aug 5, 2004 11:14 PM (reply 2 of 2)



 
Ahhh...thanks!

Sometimes I can't see the forest for the trees. I got my "logic engine" going in the wrong direction and missed the way the last split (2-2 to $) worked. Very clear explanation.
 
This topic has 2 replies on 1 page.