aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about String split() ... (was DOUBT) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about String split() ... (was DOUBT)" Watch "Question about String split() ... (was DOUBT)" New topic
Author

Question about String split() ... (was DOUBT)

sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
string str="aaaaaaaaabb";
String[]s=str.split(a{3});
system.out.println(s.length);
Can you please help me ou why the output is 4


[HENRY: Added better subject title]
[ May 07, 2007: Message edited by: Henry Wong ]
Giovanni Murguia
Greenhorn

Joined: May 04, 2007
Posts: 6
Hello!

I think the code is:


You're using as separator 3 a's (aaa) so when it begins the search it uses the first 3 a's, the string before this would be an empty string. It removes those 3 a's and now search using "aaaaaabb". It does that with the other aaa's until the last string is aaab. Using split gives 2 tokens: an empty string and "bb". Try compiling this:



Hope it helps.

Giovanni
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
The code you provided doesn't compile, please check your syntax before posting. This is the correct code with an added line so it's better to see what happens here. The regex you use is the same as "aaa".

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Does following code give any helping idea:



Output:
><
><
><
><
><
><
>bb<
7
-------------------------------
0 >aaa<
3 >aaa<
6 >aaa<
9 >aaa<
12 >aaa<
15 >aaa<



Thanks,


cmbhatt
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Chandra:
try to change string in your example to:


Results of split and Matcher are quite different.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks John,

I see the difference; But I also tried this:


String str = "bbaaaaaaaaaaaaaaaaaab";


Output:
>bb<
><
><
><
><
><
>b<
7

Could you put some focus on that?


Thanks,
[ May 07, 2007: Message edited by: Chandra Bhatt ]
sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
Hi
chandra could you explain on the output you got
I guess one more has to come before <b>
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
it depends on whether limit parameter was supplied.

from Pattern::split(...)



if there is no limit parameter, split will chop off all empty matches from the end, until non-empty one is found

Try the same string, but put extra parameter for limit (let's say 100) and you should get all matches.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
I agree John,



Output:
0>bb<
1><
2><
3>aaaaaaaaaaaa<


Change line #1 with
String[] sarr = str.split("a{3}",10);

Output:
0>bb<
1><
2><
3><
4><
5><
6><
7><

and without passing the LIMIT the out:

0>bb<

It means without limit all the empty matches are chopped off.


Thanks,
sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
No chandra
Am not able to follow,please can you explian me from start
Thanks
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Originally posted by Chandra Bhatt:

It means without limit all the empty matches are chopped off.


.. from the end only.
"bbaaaaaaaaaaaaaaaaaabb" will have empty mathces and you don't need to set any limit.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about String split() ... (was DOUBT)