How to split above String value in to an array with 7 elements. 3rd, 5th and 6th will have null as a value.
Above code generate array with 5 elements only. Last two elements with null value are not getting generated.
My blood is tested +ve for Java.
Darryl Burke wrote:
John Laker wrote:
Can you explain that code a little bit ? What does the 1 do ?
It's all there n the API for String#split(...)
The limit parameter controls the number ... If n is nonpositive then ...
Yes I read it, but had difficulty undestanding this quote there
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 nonpositive 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.
I'm not sure what pattern they are talking about and how it is applied to what.
 1
John Laker wrote:
Yes I read it, but had difficulty undestanding this quote there
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 nonpositive 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.
I'm not sure what pattern they are talking about and how it is applied to what.
The 1st argument to split() is a regex pattern.
And if the 2nd parameter is negative (1), nothing is discarded. So the result is an array with 7 elements with the last 2 are empty stringS. These last 2 elements will be discarded if split("[]") or split ("[]", 0) are used
T. Huy Nguyen wrote:
The 1st argument to split() is a regex pattern.
And if the 2nd parameter is negative (1), nothing is discarded. So the result is an array with 7 elements with the last 2 are empty stringS. These last 2 elements will be discarded if split("[]") or split ("[]", 0) are used
I see. The second part makes sense now.
Another question, What is a regex pattern ? I understand that in this example it is "" which is used to split the string, but what is like a simple definition. Google search did not yield satisfactory results.
 1
But I think a bit of wiki and the javadoc of Pattern class (http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html) are good enough.
 1
As you can see, regular expressions can become illegible very quickly. To fully understand them you should probably look up a tutorial on regular expressions.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
The City calls upon her steadfast protectors. Now for a tiny ad:
Thread Boost  a very different sort of advertising
https://coderanch.com/t/674455/ThreadBoostfeature
