July 22, 2011 [Issue 6361] New: To avoid a newline in multi-line strings | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=6361 Summary: To avoid a newline in multi-line strings Product: D Version: unspecified Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nobody@puremagic.com ReportedBy: bearophile_hugs@eml.cc --- Comment #0 from bearophile_hugs@eml.cc 2011-07-22 03:16:03 PDT --- Multi-line strings are handy, but I have a small problem with them. This is an example, it has a problem, there is an unwanted newline at the beginning (the situation is the same if you use ` instead of "): string table = " - First item: 150 - Second item: 200 - Third item: 105"; (If you are just writing such string then you can use just write() followed by a flush.) To avoid it you can write this, but both break the alignment in the source code, and they are not nice looking: string table = "- First item: 150 - Second item: 200 - Third item: 105"; string table = "- First item: 150 - Second item: 200 - Third item: 105"; This solution adds one ending newline instead: writeln(q"EOS - First item: 150 - Second item: 200 - Third item: 105 EOS"); To solve that problem in Python you use this (in Python """ or ''' denote a multi-line string): table = """\ - First item: 150 - Second item: 200 - Third item: 105""" The extra slash at the beginning avoids the start newline. I think it's worth adding this little Python syntax detail to D too. ------------- Note: this syntax is not meant to solve the more general problem in presence of indentation. In this case you probably need a library solution to de-indent, etc: void foo() { if(blah) { writeln("- First item: 150 - Second item: 200 -- Subitem 1 -- Subitem 2 - Third item: 105"); } } (Thanks to Nick Sabalausky and Andrej Mitrovic for the suggestions.) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 23, 2011 [Issue 6361] To avoid a newline in multi-line strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=6361 --- Comment #1 from bearophile_hugs@eml.cc 2011-07-23 09:12:53 PDT --- Code by Andrej Mitrovic: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=141106 It shows better why this very small change in D language is not handy to do with a library solution: import std.algorithm; import std.stdio; import std.string; string stripNewlines(string text) { auto x = text.countUntil("\n"); auto y = text.lastIndexOf("\n"); return text[x+1..y]; } template EOS(string text) { enum EOS = stripNewlines(text); } void main() { writeln(EOS!" - First item: 150 - Second item: 200 - Third item: 105 "); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation