www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Printing a quoted string

reply Amit <a a.a> writes:
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
```
Jan 02 2022
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 1/2/22 9:27 AM, Amit wrote:

 string s = "one \"two\"\nthree four";
The issue there is that the string does not contain the two characters \" but the single character ". So, that's a syntax issue. The solution is to use back ticks to tell the compiler what you really mean.
 And get as output

 ```
 "one \"two\"\nthree four"
 ```
import std.stdio; void main() { string s = `one \"two\"\nthree four`; // <-- Back ticks writeln(s); } Ali
Jan 02 2022
parent Amit <a a.a> writes:
On Sunday, 2 January 2022 at 17:33:22 UTC, Ali Çehreli wrote:
 The issue there is that the string does not contain the two 
 characters \" but the single character ". So, that's a syntax 
 issue. The solution is to use back ticks to tell the compiler 
 what you really mean.
Thank you! I think my question was misunderstood though. I am not looking to change the **input** string (as the backticks would do), but the **output** string. Like this: ``` assert(some_formatting("one \"two\"\nthree four") == `"one \"two\"\nthree four"`); ``` I hope this clarifies :)
Jan 02 2022
prev sibling next sibling parent reply WebFreak001 <d.forum webfreak.org> writes:
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"] ```
Jan 02 2022
parent reply Amit <a a.a> writes:
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 ^_^
Jan 02 2022
parent reply Caten <catenhu gmail.com> writes:
On Sunday, 2 January 2022 at 21:16:55 UTC, Amit wrote:
 On Sunday, 2 January 2022 at 19:26:50 UTC, WebFreak001 wrote:
 [...]
On Sunday, 2 January 2022 at 19:37:38 UTC, JG wrote:
 [...]
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 ^_^
Hi, I also need a function to "unquote" string, like this: ```d assert(unquote(`\n`)=="\n"); ``` Is there a way to do that?
Mar 20 2022
next sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Sunday, 20 March 2022 at 09:42:44 UTC, Caten wrote:
 On Sunday, 2 January 2022 at 21:16:55 UTC, Amit wrote:
 On Sunday, 2 January 2022 at 19:26:50 UTC, WebFreak001 wrote:
 [...]
On Sunday, 2 January 2022 at 19:37:38 UTC, JG wrote:
 [...]
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 ^_^
Hi, I also need a function to "unquote" string, like this: ```d assert(unquote(`\n`)=="\n"); ``` Is there a way to do that?
```d void main() { import std.array : replace; auto q = `Hello,\n deneme`; auto p = "Hello,\n deneme"; assert(q.replace("\\n", "\n") == p); } ``` SDB 79
Mar 20 2022
prev sibling parent reply cc <cc nevernet.com> writes:
On Sunday, 20 March 2022 at 09:42:44 UTC, Caten wrote:
 Hi, I also need a function to "unquote" string, like this:
 ```d
 assert(unquote(`\n`)=="\n");
 ```
 Is there a way to do that?
I rolled my own for that recently: ```d string dequote(string str) safe pure { if (str.length < 2) return str; if ((str[0] == '"' || str[0] == '\'' || str[0] == '`') && str[$-1] == str[0]) { return str[1 .. $-1]; } return str; } ```
Mar 22 2022
parent cc <cc nevernet.com> writes:
On Tuesday, 22 March 2022 at 07:18:00 UTC, cc wrote:
 On Sunday, 20 March 2022 at 09:42:44 UTC, Caten wrote:
 Hi, I also need a function to "unquote" string, like this:
 ```d
 assert(unquote(`\n`)=="\n");
 ```
 Is there a way to do that?
I rolled my own for that recently: ```d string dequote(string str) safe pure { if (str.length < 2) return str; if ((str[0] == '"' || str[0] == '\'' || str[0] == '`') && str[$-1] == str[0]) { return str[1 .. $-1]; } return str; } ```
Oops, I misread what you were asking for. Was thinking quotes like quotation marks, not backslashes.
Mar 22 2022
prev sibling parent JG <someone somewhere.com> writes:
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])); } ```
Jan 02 2022