If I have a String like "Field 1\tField 2\tField 3\t" and I use
String.split("\t"), I'll get an array with 3 elements. I'm working with
data that is used by both Perl and Java programs. Perl handles it
differently and, in this case, it works better. If I split the same string
in Perl, I'll get 4 elements in an array with the last one being an empty
string.
I'd like Java to do the same thing. I know I could do something like this:
String myString;
String[] myData;
boolean nullFlag = false;
...
if (myString.endsWith("\t") {
nullFlag = true;
myString = myString + " ";
}
myData = myString.split("\t");
if (nullFlag) {
myData[myData.length -1] = "";
}
I know that'll work, but from what I read, this *SHOULD* work all the time.
It seems to work, but I'd just like someone to confirm that my
understanding is correct.
myData = myString.split("\t", -1);
From what I understand, using any negative number for the 2nd argument
should make sure as many elements are generated as possible, including an
empty string at the end. Tests seem to make me think so and the Javadoc
indicates it as well, but I'd like it if someone could verify that my
understanding is correct and that the negative number can be any int less
than 0.
Thanks!
Hal
Knute Johnson - 29 Jan 2008 04:20 GMT
> If I have a String like "Field 1\tField 2\tField 3\t" and I use
> String.split("\t"), I'll get an array with 3 elements. I'm working with
[quoted text clipped - 34 lines]
>
> Hal
Hal:
I think you are correct at least that is the way that I read it. And
testing it gives that result as well.

Signature
Knute Johnson
email s/nospam/knute/
------->>>>>>http://www.NewsDem
Hal Vaughan - 29 Jan 2008 06:11 GMT
>> If I have a String like "Field 1\tField 2\tField 3\t" and I use
>> String.split("\t"), I'll get an array with 3 elements. I'm working with
[quoted text clipped - 40 lines]
> I think you are correct at least that is the way that I read it. And
> testing it gives that result as well.
Thanks. I've had times where I've read the Javadoc and found out I was
misinterpreting what I thought was obvious and I just wanted to be sure.
Hal