This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split using tab delimiter

 
lava ayan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to read a file line by line and
use the java split funtion on tab , to split a line into individual fields.

Eg: File contains

Field1 Field2 Field3
Sun Moon Star
Moon

I use split("/t", 3). It is fine when the line has 3 fields

When the the field1 is empty and there is a tab before field2, it ignores the tab.
When the last field is empty but if there is a tab after second field , it ignores the tab.

I want to be able to use the split function to recognize the fact that there was an empty string when the first or last field is empty.

It is also my observation that , when I use a split with pipe delimiter (|) , it recognizes the absence of the first and last field.

ie.
Field1 | Field2 | Field3
Sun | Moon | Star
|Moon|

Am I missing something when using the tab with split , that will give me the same behavior as other delimiters

like(|)?
 
Moojid Hamid
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I missing something when using the tab with split , that will give me the same behavior as other delimiters


A little testing shows exact same results for both delimiters:




Here is what is get:


To solve the problem of empty trailing fields you can initialize variables with default values(or empty strings).
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> When the the field1 is empty and there is a tab before field2, it ignores the tab.

works OK for me, sure its a tab you have there

> When the last field is empty but if there is a tab after second field , it ignores the tab.

use the -1 argument in split()


 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume by "split" you mean String#split()? If you read the API it tells you what the individual members of the array are:
. . . 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.
So it you start with the delimiter, what will the first member of the array be?
 
lava ayan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic