Re: Bug in String's split method???



castillo.bryan@xxxxxxxxx wrote:
> 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.*

--
Virgil


.



Relevant Pages

  • Comments on Comments (was Re: Getting to 100 (#119))
    ... # yield each partitioning of the receiver into count partitions ... # an initial substring of increasing length, ... # the string into count-1 partitions. ... #:ops - an array of strings representing the operators to be inserted into ...
    (comp.lang.ruby)
  • Re: Open File For Input Method Faster Than File System Object Stream?
    ... Much depends on exactly what you are doing, and the best method to advise would depend chiefly on whether you are interested in only the first occurrence of the substring in the file or whether you want to check every line of the file for that substring. ... Dim s1 As String, fn As Long ... You could then use Instr in a loop to run through the entire string just once, searching for and recording (in an array of Longs) the position of every linefeed character it finds. ...
    (microsoft.public.vb.general.discussion)
  • Re: How to chck for string in array elements
    ... thends to get quite large, I want to optimize my algorithm som that if ... $string is a part of a element already stored in @words it won't be ... a substring of an element in the array. ...
    (comp.lang.perl.misc)
  • Re: How to chck for string in array elements
    ... thends to get quite large, I want to optimize my algorithm som that if ... But I cant figure out an efficient way to check if $string is ... a substring of an element in the array. ...
    (comp.lang.perl.misc)
  • Re: Get first N words of a string
    ... array with the incorrect number of dimensions. ... I know my variable is a string. ... How to create a substring of a string with all the words until the ... could do instead is find the Nth space. ...
    (microsoft.public.dotnet.framework.aspnet)