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 / First Aid / January 2008

Tip: Looking for answers? Try searching our database.

String.split() -- If Last String is Empty?

Thread view: 
Hal Vaughan - 28 Jan 2008 07:50 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
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


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.