[Issue 6361] New: To avoid a newline in multi-line strings
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Jul 22 03:16:07 PDT 2011
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 at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at 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: -------
More information about the Digitalmars-d-bugs
mailing list