• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic