• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does following code give any helping idea:



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



Thanks,
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra:
try to change string in your example to:


Results of split and Matcher are quite different.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
chandra could you explain on the output you got
I guess one more has to come before <b>
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No chandra
Am not able to follow,please can you explian me from start
Thanks
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic