2

i've one text file, and im doing something like this :

resultingTokens = currentLine.split("\\t"); 

file data is tab delimited. But when I parse it with above code, it does not give expected output. I realize that tab is actually editor specific. But how does Java (above code) interprets it?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
Milan Mendpara
  • 3,091
  • 4
  • 40
  • 60
  • 2
    The tab character is well-defined - it isn't editor-specific. You've said that you're not getting the expected output - but what *are* you getting? And why are you escaping the backslash instead of just using `"\t"`? – Jon Skeet Mar 31 '12 at 10:00
  • 1
    Duplicate http://stackoverflow.com/questions/1635764/string-parsing-in-java-with-delimeter-tab-t-using-split – Griffin Mar 31 '12 at 10:02

2 Answers2

3

you are trying to split on \t (literally backslash followed by lower case T) because you're escaping the backslash. a single backslash with a t will represent a tab.

resultingTokens = currentLine.split("\t"); 

is what will give you the result you were expecting.

dldnh
  • 8,923
  • 3
  • 40
  • 52
1

What you are looking for is

resultingTokens = currentLine.split("\t");

(note the single backslash.)

What you have right now is a two-character string: a single backslash followed by the lowercase letter t.

What I am proposing above is a single-character string that consists of the tab character.

NPE
  • 486,780
  • 108
  • 951
  • 1,012