• 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

split()...

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,i got it from whizlab practice exam 4.


output:4
I am not getting how the split() works here?

If i change the above code as
String str="aa";
String[] s=str.split("a{3}");
gives output as 1.

I am really confused ,because it should be three occurences of "a" but here only "aa" are in the string str.
please expalin me..

Thanks
Preetha

( Jesper Young: Added code tags )
[ December 15, 2008: Message edited by: Jesper Young ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If i change the above code as

String str="aa";
String[] s=str.split("a{3}");
gives output as 1.

I am really confused ,because it should be three occurences of "a" but here only "aa" are in the string str.
please expalin me..



Keep in mind that the split() method looks for a match as a delimiter. Since there is only two a's, there are no match for delimiters. Since there are no delimiters, the orignal string is kept intact as the result.

Henry
[ December 14, 2008: Message edited by: Henry Wong ]
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output:4
I am not getting how the split() works here?



The argument to split function is a regular expression for the delimiter it uses to break the string(or identify the sub string). Here, the delimiter specified is "aaa" through the regex "a{3}" so split would identify a total of four sub-strings as:
"_aaa_aaa_aaa_" where "_" would represent the sub-strings created.

Total number of underscores gives you the length.
[ December 14, 2008: Message edited by: Monu Tripathi ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank Henry and Monu!
i got it.


Preetha
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post code.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
{} syntax is not given in K&B book so I don't think it will come in the exam. But it is good to have some extra knowledge
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic