www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to read \n from a string

reply "Stephen Jones" <siwenjo gmail.com> writes:
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?
Nov 23 2013
next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
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.
Nov 23 2013
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Nov 23 2013
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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!
Nov 23 2013