user623990
user623990

Reputation:

Handling the TAB character in Java

I'm going through a configuration file with a RandomAccessFile reader. I have a configuration option which is one (1) tab away from the start of the line. When my reader gets this line, would I be able to just tell it to skip one character and then start reading, or does the tab character not work that way?

Example:

This is a line
        This line has a tab

Let's say I've loaded the second line into my reader. If I'm playing with that String and I do currentLine = currentLine.subString(1);

Would that give me:

currentLine = "This line has a tab";

Thanks for your help.

Upvotes: 27

Views: 129365

Answers (3)

live-love
live-love

Reputation: 52416

You can also use the tab character '\t' to represent a tab, instead of "\t".

char c ='\t'; // tabulator
char c =(char)9; // tabulator

Upvotes: 10

vickirk
vickirk

Reputation: 4067

Or you could just perform a trim() on the string to handle the case when people use spaces instead of tabs (unless you are reading makefiles)

Upvotes: 3

Richard H
Richard H

Reputation: 39065

Yes the tab character is one character. You can match it in java with "\t".

Upvotes: 63

Related Questions