digitalmars.D.learn - How to read \n from a string
- Stephen Jones (11/11) Nov 23 2013 I want to be able to write a string containing \n to indicate
- Jesse Phillips (12/24) Nov 23 2013 There is no distinction between the two. '\n' is o new line,
- bearophile (13/25) Nov 23 2013 Do you like this?
- H. S. Teoh (15/21) Nov 23 2013 Inserting an actual line break inside a string literal is equivalent to
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
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
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
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