• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Doubt in split()

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please help me to undertsand why the output of the program is 4.

public class splitTry {
public static void main(String[] args) {
String str="aaaaaaaaabb";
String[] s=str.split("a{3}");
System.out.println(s.length);
}
}
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str="aaaaaaaaabb";

When you split the above string using the given crieria(alpha numeric character at every 3rd position), it splits the String like this: "aaa", "aaa","aaa","bb"

and stores it in array at 0,1,2,3 index so the length is 4.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String = "boo:and:foo"
Calling String.split() with following regex:

RegexResult
:{ "boo", "and", "foo" }
o{ "b", "", ":and:f" }

When regex is "o" an empty string is created when "oo" is found in boo. Why is an empty string not found when the String splits on the last two "oo"??
In other words y is the array length 3 and not 4 with two empty strings?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call the split function with only one argument ,then it assumes that the second argument of the split function is zero. And when the second argument is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. This is the reason why you are not seeing the trailing empty strings.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@mallika arora:
The array contains after the split [, , , bb] so the length is 4.
[ October 10, 2007: Message edited by: Roland Schneider ]
 
I'm still in control here. LOOK at this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic