I'm coding an IDE. Let's say I have the Hello, World Pascal program on a string:
String sourceCode = "program Hello;\nbegin\nwriteln ('Hello, world.');\nend.";
If I do System.out.println(sourceCode); the output shows:
program Hello;
begin
writeln ('Hello, world.');
end.
Great, but I want to show new lines as \n. I tried:
sourceCode = sourceCode.replaceAll(System.lineSeparator(), "\\n");
But then System.out.println(sourceCode); outputs:
program Hello;nbeginnwriteln ('Hello, world.');nend.
When I expected \n to be shown:
program Hello;\nbegin\nwriteln ('Hello, world.');\nend.
How can I achieve that? Full demo:
public class PrintPascalHelloWorld {
public static void main(String[] args) {
String sourceCode = "program Hello;\nbegin\nwriteln ('Hello, world.');\nend.";
System.out.println(sourceCode);
sourceCode = sourceCode.replaceAll(System.lineSeparator(), "\\n");
System.out.println(sourceCode);
}
}
I'm able to compile and run it using Online Java IDE.