Bug in String's split method???

C

castillo.bryan

I was trying to write a utility method that work almost the same as the
split method on String, that would work with java versions 1.3 or less,

using Jakarta's ORO library. I though I implemented it according to the
javadoc for the split method on String. But the expression
"".split(",", 0),
returns an array with 1 element, I was expecting an array with no
elements.
The expression ",".split(",", 0) returns an array with no elements.
Is this a bug or am I reading the docs wrong?

Code:
String[] result = null;
result = "".split(",", 0);
System.out.println(result.length);
result = ",".split(",", 0);
System.out.println(result.length);
System.out.println(System.getProperty("java.version"));

Output:
1
0
1.4.2_06

I thing the output should be:
0
0
1.4.2_06
 
R

Remon van Vliet

I was trying to write a utility method that work almost the same as the
split method on String, that would work with java versions 1.3 or less,

using Jakarta's ORO library. I though I implemented it according to the
javadoc for the split method on String. But the expression
"".split(",", 0),
returns an array with 1 element, I was expecting an array with no
elements.
The expression ",".split(",", 0) returns an array with no elements.
Is this a bug or am I reading the docs wrong?

Code:
String[] result = null;
result = "".split(",", 0);
System.out.println(result.length);
result = ",".split(",", 0);
System.out.println(result.length);
System.out.println(System.getProperty("java.version"));

Output:
1
0
1.4.2_06

I thing the output should be:
0
0
1.4.2_06

Why? the regex expression is not found in your string, so it cant be split,
you called the split() method on a string, so if no splitting occurs there's
always one part left; the original string.
 
V

Virgil Green

I was trying to write a utility method that work almost the same as
the split method on String, that would work with java versions 1.3 or
less,

using Jakarta's ORO library. I though I implemented it according to
the javadoc for the split method on String. But the expression
"".split(",", 0),
returns an array with 1 element, I was expecting an array with no
elements.
The expression ",".split(",", 0) returns an array with no elements.
Is this a bug or am I reading the docs wrong?

Code:
String[] result = null;
result = "".split(",", 0);
System.out.println(result.length);
result = ",".split(",", 0);
System.out.println(result.length);
System.out.println(System.getProperty("java.version"));

Output:
1
0
1.4.2_06

I thing the output should be:
0
0
1.4.2_06

From java doc for String.split (asterisks/bold added for emphasis):

The array returned by this method contains each substring of this string
that is terminated by another substring that matches the given expression or
is terminated by the end of the string. The substrings in the array are in
the order in which they occur in this string. *If the expression does not
match any part of the input then the resulting array has just one element,
namely this string.*
 
C

castillo.bryan

Yes I saw that part, but the part that throws me is the limit
parameter. Here are the docs on that. Not the asterisks.

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.*

------

I think that the poriton you emphasized "f the expression does not
match any part of the input then the resulting array has just one
element,
namely this string." and the portion I emphasized "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."
conflict with eachother in the case

"".split(",", 0);

The limit was zero so the empty trailing field should have been
discarded. I could read the docs both ways. The docs are ambiguous on
this feature.
 
V

Virgil Green

Yes I saw that part, but the part that throws me is the limit
parameter. Here are the docs on that. Not the asterisks.

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.*

------

I think that the poriton you emphasized "f the expression does not
match any part of the input then the resulting array has just one
element,
namely this string." and the portion I emphasized "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."
conflict with eachother in the case

"".split(",", 0);

The limit was zero so the empty trailing field should have been
discarded. I could read the docs both ways. The docs are ambiguous
on this feature.

I see no conflict or ambiguity. As you pointed out, *trailing* empty strings
will be discarded. There is no trailing empty string until there is a
delimiter. The beginning of the string is not a delimiter with respect to
the pattern, IMHO.

The section I quoted above is the rule to be used in a special case... when
there are no pattern matches. The second section you quoted is only in force
when there is at least one pattern match. I assume it was intended to
prevent the return of an empty string element when the pattern matched the
tail characters of the string.
 
C

castillo.bryan

Virgil said:
I see no conflict or ambiguity. As you pointed out, *trailing* empty strings
will be discarded. There is no trailing empty string until there is a
delimiter. The beginning of the string is not a delimiter with respect to
the pattern, IMHO.
The section I quoted above is the rule to be used in a special case... when
there are no pattern matches. The second section you quoted is only in force
when there is at least one pattern match. I assume it was intended to
prevent the return of an empty string element when the pattern matched the
tail characters of the string.

I see nothing in the docs that mention the removal of trailing empty
strings, is only in force when there is at least 1 pattern match.
 
V

Virgil Green

I see nothing in the docs that mention the removal of trailing empty
strings, is only in force when there is at least 1 pattern match.

I submit that the definition of "trailing" implies that there is something
to "trail" and that the "something" must be at least one pattern match. The
documentation indicating that empty strings are removed is *only* predicated
upon those empty strings be *trailing* empty strings. What does you example
empty string trail behind?

Anyway, you may choose to use any definition of "trailing" that justifies
your conclusion that there is a bug or that the documentation is ambiguous.
I will continue to use the one that indicates there is something (a matched
token) to be trailing behind, the one that fits the documented (as I read
it) and actual behavior of the class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top