aspose file tools*
The moose likes Java in General and the fly likes String's split() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Watch "String New topic
Author

String's split()

Chris Cairns
Ranch Hand

Joined: Jan 31, 2003
Posts: 48
Hi all,
Any help would be appreciated.
My question concerns the split method in the String class.
Let's pretend I have a sequence of Strings in a text file that are tabbed delimited. And some of the Strings are simply empty but are still delimited by a tab. For example, Hello [tab] how [tab] [tab] you [tab] ? (Obviously this person is a foreigner with bad grammar and forgot "are".) After reading this line in and converting it to a String object, I use the split method. My question is this: how does the split method handle the blank/empty string? What character data does it return? Because when I try to print the bytes of this, no bytes are printed.
I have some follow up questions, but I'll wait for some answsers first.

Thanks!
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Can you show us the code that did not work?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Chris, the split method returns an empty String. This probably means that no bytes are associated with the byte array within the String ( getBytes() returns a "new byte[0]" )
Jamie
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You might want to try split("\t+"). This will treat multiple consecutive tabs as a single delimiter, and so the blank field will be skipped entirely. IF that's what you really want - quite possibly, it's not.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String's split()