Printing a quoted string

WebFreak001 d.forum at webfreak.org
Sun Jan 2 19:26:50 UTC 2022


On Sunday, 2 January 2022 at 17:27:53 UTC, Amit wrote:
> Hi!
>
> I would like to print a string in the same format that I would 
> write it in the code (with quotes and with special characters 
> escaped). Similar to [Go's %q 
> format](https://pkg.go.dev/fmt#hdr-Printing). Is there a safe, 
> built-in way to do that?
>
> For example:
>
> ```
> string s = "one \"two\"\nthree four";
> writeln(/* ??? */);
> ```
>
> And get as output
>
> ```
> "one \"two\"\nthree four"
> ```
>
> Instead of
>
> ```
> one "two"
> three four
> ```

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"]
```


More information about the Digitalmars-d-learn mailing list