Sasha
Sasha

Reputation: 1570

New line character as part of the String

I've got the problem creating String looking as follows:

"SSR LANG SA  HK1/EN;S7;P1\n"+

Code that does that should append \n as part of the string + quote + append string literal + actual new line literal:

javaFormattedText.append("   \""+ tokenizer.nextElement() + "\\n\"+\n");

The output String should look:

"SSR LANG SA  HK1/EN;S7;P1\n"+

But it looks:

"SSR LANG SA  HK1/EN;S7;P1
\n"+

Therefore java complains about unclosed string literal.

The purpose of doing it is generating test classes dynamically and those contain a lot of text information that needs to be java formatted strings.

How can achieve the target?

UPD

Solved the problem by doing this:

String line = (String) AIR_textTokenizer.nextElement();

line = line.replace("\n", "").replace("\r", "");

Upvotes: 0

Views: 2308

Answers (2)

maowtm
maowtm

Reputation: 2012

Your tokenizer.nextElement() is probably returning a trailing new line. To remove that:

String str = tokenizer.nextElement();
if (str.endsWith('\n')) {
    str = str.substring(0, str.length - 1);
}
javaFormattedText.append("   \""+ str +"\\n\"+\n");

Upvotes: 5

amkz
amkz

Reputation: 588

If you watn to add line separator use: System.lineSeparator();

Upvotes: -1

Related Questions