Milan Mendpara
Milan Mendpara

Reputation: 3131

how to split string with "\\t" in java

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?

Upvotes: 2

Views: 1687

Answers (2)

dldnh
dldnh

Reputation: 8961

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.

Upvotes: 3

NPE
NPE

Reputation: 500953

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.

Upvotes: 1

Related Questions