Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Bug in String's split method???

Thread view: 
castillo.bryan@gmail.com - 27 Jun 2005 07:43 GMT
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
Remon van Vliet - 27 Jun 2005 12:35 GMT
> 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,
[quoted text clipped - 24 lines]
> 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.
Virgil Green - 27 Jun 2005 18:37 GMT
> 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
[quoted text clipped - 25 lines]
> 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.*

Signature

Virgil

castillo.bryan@gmail.com - 28 Jun 2005 01:21 GMT
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.
Virgil Green - 28 Jun 2005 13:40 GMT
> Yes I saw that part, but the part that throws me is the limit
> parameter.  Here are the docs on that.  Not the asterisks.
[quoted text clipped - 25 lines]
> 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.

Signature

Virgil

castillo.bryan@gmail.com - 28 Jun 2005 15:51 GMT
> > Yes I saw that part, but the part that throws me is the limit
> > parameter.  Here are the docs on that.  Not the asterisks.
[quoted text clipped - 35 lines]
> 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.
Virgil Green - 28 Jun 2005 16:41 GMT
>>> Yes I saw that part, but the part that throws me is the limit
>>> parameter.  Here are the docs on that.  Not the asterisks.
[quoted text clipped - 41 lines]
>> --
>> Virgil

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.

Signature

Virgil



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.