• 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

Doubt regarding Split(regex,limit) method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The following is program from one of the mock test.

public class SplitTest {
public static void main(String[] args) {
String s = "Aaaaah, This is Program";
String[] s1 = s.split("a", ?); // ?
for(String str:s1)
System.out.print(str);
}
}
If the required output of program is : Ah, This is Program

Then what should be the value of "?" in the split() method?
I have selected the value of limit as "4" but answer was wrong. The correct value is "5".
Can anyone explains how come the value of limit is 5 instead of 4,as "aaaa" "a" expression has occured 4 times in the given string?

Thanks in Advance.

Regards,
Varsha
 
author
Posts: 23956
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


I have selected the value of limit as "4" but answer was wrong. The correct value is "5".
Can anyone explains how come the value of limit is 5 instead of 4,as "aaaa" "a" expression has occured 4 times in the given string?



If you review the code, you'll notice that it splits the string and then prints it out together -- effectively, it just removes the a's as delimiters. There are 5 a's, and you need to remove the first 4, which means that you need to apply the pattern 4 times.

So ... from the JavaDoc.


The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.



So, to have the pattern applied 4 times, the value of the limit must be 5. Or in other words, the limit is the maximum number of tokens -- which in this case is 5.

Henry
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Can you please elaborate the same point in simple language..!!
It did not stuck in my mind...just passed through...though i tried my best..!!
 
Henry Wong
author
Posts: 23956
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

Originally posted by Sunny Jain:

Can you please elaborate the same point in simple language..!!
It did not stuck in my mind...just passed through...though i tried my best..!!




Let's assume that you are right and the answer is 4. Then according to the Javadoc....

If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n



The means that the pattern will be applied 3 times (n-1) and the resultant array will be of size 4 (max). So the resultant array is....



And when you print this out using the specified loop, you get...
"Aah, This is Program". Which is not the required output.

Henry
[ February 23, 2008: Message edited by: Henry Wong ]
 
Varsha Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.... The explaination helped me a lot to understand the logic...

Thanks and Regards,
Varsha
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic