Regards, Abdul Mohsin
splitting with default limit -> 0
=================================
>< //extra line because of the default limit "0"
>a<
>p<
>p<
>l<
>e<
>s<
splitting with limit -> 1
=================================
>apples<
splitting with limit -> -1
=================================
>< //extra line 1
>a<
>p<
>p<
>l<
>e<
>s<
><//extra line 2
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.
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.
If n 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.
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
posted Today 10:43 AM Profile for Abdul Mohsin Email Abdul Mohsin Send New Private Message Edit/Delete Post Reply With Quote Hi,
String str = "apples";
String s[] = str.split("");
for (String i:s)
System.out.println(">" + i + "<");
output:
>< // line 1 not clear
>a<
>p<
>p<
>l<
>e<
>s<
Regards, Abdul Mohsin
String str = "apples";
String s[] = str.split("");
for (String i:s)
System.out.println(">" + i + "<");
output:
>< // line 1 not clear
>a<
>p<
>p<
>l<
>e<
>s<
Regards, Abdul Mohsin
Regards, Abdul Mohsin
Regards, Abdul Mohsin
Guess the output of :
code 1
code:
String str = "aaa";
String s[] = str.split("a");
for (String i:s)
System.out.println(">" + i + "<");
code 2
code:
String str = "aaab";
String s[] = str.split("a");
for (String i:s)
System.out.println(">" + i + "<");
Regards, Abdul Mohsin
Regards, Abdul Mohsin
String str="aaa";
String s[] = str.split("a");
---------------------------------
|--------
|a|a|a| |
|--------
here the matcher applies 3 times
so we will get like this
<>
<>
<>
<>
Regards, Abdul Mohsin
Regards, Abdul Mohsin
Regards, Abdul Mohsin
Originally posted by anil kumar:
Hey
-------------------------------
String str="aaa";
String s[] = str.split("a");
---------------------------------
|--------
|a|a|a| |
|--------
here the matcher applies 3 times
so we will get like this
<>
<>
<>
<>
Now the split with ZERO argument will be applied
so it finds the last empty string,it removes that and agian it sees that one more empty string it finds it removes that one and so on.Utill all the trailing empty string removed
------------------------------------
String str="aaab";
String s[] = str.split("a");
--------------------------------------
<>
<>
<>
<b>
<>
Now the split with ZERO argument will be applied
so it finds the last empty string, and again it tries to apply but this time it fails because it finds <b>
so out put is
<>
<>
<>
<b>
Thanks
Anil Kumar
[ May 23, 2007: Message edited by: anil kumar ]
Regards, Abdul Mohsin
cmbhatt
Regards, Abdul Mohsin
Regards,<br />Gautam Arora<br />100% Geek
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. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n 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.
The input "boo:and:foo", for example, yields the following results with these parameters:
Parameters:
input - The character sequence to be split
limit - The result threshold, as described above
Returns:
The array of strings computed by splitting the input around matches of this pattern
cmbhatt
Regards,<br />Gautam Arora<br />100% Geek
Input "boo:and:foo"
Regex Limit Result
o 5 { "b", "", ":and:f", "", "" } (as given)
cmbhatt
Don't get me started about those stupid light bulbs. |