how do I add a line break in a string?

Jens Schauder picture Jens Schauder · Oct 7, 2014 · Viewed 15.6k times · Source

The question is in the title.

If I do this in the REPL (SML/NJ in windows commandline)

val x = "hello\nworld";

I'd expect

val x = "hello
world" : string

or something similar, but I get

val x = "hello\nworld" : string

although I found somewhere that \n is a line break in SML strings.

Answer

Sebastian Paaske Tørholm picture Sebastian Paaske Tørholm · Oct 7, 2014

When the string is displayed, the line break is displayed as \n.

However, if you try printing the string, you'll see it as a proper line break:

- val x = "hello\nworld";
> val x = "hello\nworld" : string
- print x;
hello
world> val it = () : unit

It's simply how the string is displayed, and you cannot make the SML interpreter display it as an actual newline except by printing it yourself.