How to read \n from a string

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Nov 23 17:55:49 PST 2013


On Sun, Nov 24, 2013 at 01:40:43AM +0100, Stephen Jones wrote:
> I want to be able to write a string containing \n to indicate
> newline breaks, but I want the string to cover multiple lines for
> example:
> 
> string str = "This is just a little string I wrote\n to see if all
> was upside down or not, or known to be back to front at all.";

Inserting an actual line break inside a string literal is equivalent to
writing "\n" inside a single-line string.

If you want a string literal that's multi-line in the source code but
not in its actual value, use the ~ operator, like this:

	// This string doesn't contain any newline characters.
	string str = "blah blah blah "~
		"more more more "~
		"end";

Using ~ explicitly has the nice side-effect that you can indent your
literal nicely, but the indentation spaces are not part of the string's
value, so you can indent freely.


T

-- 
No! I'm not in denial!


More information about the Digitalmars-d-learn mailing list