Printing a quoted string

JG someone at somewhere.com
Sun Jan 2 19:37:38 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
> ```

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]));

}
```



More information about the Digitalmars-d-learn mailing list