On Thu, Mar 31, 2016 at 03:23:52AM +0000, Seb via Digitalmars-d-learn wrote:
> On Thursday, 31 March 2016 at 03:15:49 UTC, cy wrote:
> >This might be a dumb question. How do I format a string so that all
> >the newlines print as \n and all the tabs as \t and such?
> 
> http://dlang.org/spec/lex.html#WysiwygString
> 
> r"ab\n" or `ab\n`

Or the verbose way:

        "abc\\ndef\\tghi"

But if the string is only known at runtime, you'd probably have to use
std.regex.replaceAll or something like that to manually escape
characters. Or implement manual substitution with a pipeline:

        string myString = ...;
        string escapedStr = myString
                .chunks(1)
                .map!(c => (c == "\n") ? "\\n" :
                           (c == "\r") ? "\\r" :
                           (c == "\t") ? "\\t" :
                           c)
                .joiner
                .array;


T

-- 
Береги платье снову, а здоровье смолоду. 

Reply via email to