How to read \n from a string

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Sat Nov 23 17:21:10 PST 2013


On Sunday, 24 November 2013 at 00:41:00 UTC, 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.";
>
> if I use:
>
> foreach(c; str){
>   if(c == '\n') writeln("new line");
> }
>
> I get 2 prints of "new line", one for the \n and one for the 
> new line. Is there any way to isolate the explicit \n?

There is no distinction between the two. '\n' is o new line, 
unless you're in DOS then it is "\r\n" but that is a different 
issue.

What you need to do is remove the new lines you don't want, this 
can be done through concatenation:

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.";

The \n character is an instruction to the compiler to place a 
new-line character in the string, while pressing return creates 
the actual character in the file.


More information about the Digitalmars-d-learn mailing list