Print a string that understands newlines (\n), in Erlang

I have a something like this: Run = "Test run [number\n". When I do io:format("~p", [Run]), I don't see the newline (it's printed as \n in the string).

How can I print to the screen (or to a file), with characters like this understood for what they are (newlines for example)?

Upvotes: 1

Views: 4051

Answers (1)

Dogbert
Dogbert

Reputation: 222448

You need to use ~s in the format string instead of ~p.

1> Run = "Test run [number\n".
"Test run [number\n"
2> io:format("~s", [Run]).
Test run [number
ok

More details about the different control sequences allowed in io:format: http://erlang.org/doc/man/io.html#format-3

~s

Prints the argument with the string syntax. The argument is, if no Unicode translation modifier is present, an iolist(), a binary(), or an atom(). If the Unicode translation modifier (t) is in effect, the argument is unicode:chardata(), meaning that binaries are in UTF-8. The characters are printed without quotes. The string is first truncated by the given precision and then padded and justified to the given field width. The default precision is the field width.

~p

Writes the data with standard syntax in the same way as ~w, but breaks terms whose printed representation is longer than one line into many lines and indents each line sensibly. Left justification is not supported. It also tries to detect lists of printable characters and to output these as strings. The Unicode translation modifier is used for determining what characters are printable.

Upvotes: 9

Related Questions