How to read \n from a string

bearophile bearophileHUGS at lycos.com
Sat Nov 23 17:29:35 PST 2013


Stephen Jones:

> 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?

Do you like this?


void main() {
     import std.stdio, std.array;

     auto s = `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.`;

     s.writeln;
     writeln;
     auto s2 = s.replace("\n", "").replace(`\n`, "\n");
     s2.writeln;
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list