Printing a quoted string

Amit a at a.a
Sun Jan 2 21:16:55 UTC 2022


On Sunday, 2 January 2022 at 19:26:50 UTC, WebFreak001 wrote:
> as a hack I always do:
> ```d
> writeln([s]);
> ```
> because arrays get serialized like D strings, there will be 
> additional `[` and `]` though.
>
> Sample output:
> ```
> ["Hello there \"uwu\" ``\x1B[Dabc\n"]
> ```

On Sunday, 2 January 2022 at 19:37:38 UTC, JG wrote:
> Also a bit of a hack.
>
> ```
> import std.stdio : writeln;
> import std.format : format;
>
>
>     void main()
>     {
>         string s = "one \"two\"\nthree four";
>         writeln(format("%(%s%)",[s]));
>
> }
> ```

Yes! That's what I needed.

I wrapped it in a function like so:

```d
string quote(string s) {
     return format("%s", [s])[1 .. $ - 1];
}
unittest {
     assert(quote("one \"two\"\nthree four") == `"one 
\"two\"\nthree four"`);
}
```

Thanks for your responses ^_^


More information about the Digitalmars-d-learn mailing list