Re: [eclipse-users] Howto include non-printable characters in String?

From: Joachim Schimpf <>
Date: Tue, 12 Dec 2006 17:07:32 +0000
Ulrich Scholz wrote:
> Hi,
> I'd like to create a String that includes, e.g., newlines.  Is that
> possible?  And how?

String (and atom) syntax allows the usual escape sequences
like "\n", or octal sequences "\012" or ISO hex sequences "\xa\".
The full spec is in the Syntax appendix of the User Manual

If you are unsure of what you are getting, you can check with

?- string_list("a\nb", L).
L = [97, 10, 98]
Yes (0.00s cpu)

This also works backwards to construct a string from character codes:

?- string_list(S, [97, 10, 98]).
S = "a\nb"
Yes (0.00s cpu)

To assemble strings from larger components, look at concat_string/2,3:

?- concat_string(["abc", "\n", "def", 123], S).
S = "abc\ndef123"
Yes (0.00s cpu)

If you want to build up a string incrementally, you can use
a string stream, write into it with the normal I/O builtins,
and finally retrieve the result as a single string, e.g.

?- open(string(""), write, Stream),
    write(Stream, hello),
    write(Stream, world),
    get_stream_info(Stream, name, String),
Stream = 27
String = "hello\nworld"
Yes (0.00s cpu)

-- Joachim
Received on Tue Dec 12 2006 - 17:07:47 CET

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:57 CET