digitalmars.D.learn - Printing a quoted string
- Amit (20/20) Jan 02 2022 Hi!
- =?UTF-8?Q?Ali_=c3=87ehreli?= (10/15) Jan 02 2022 The issue there is that the string does not contain the two characters
- Amit (10/14) Jan 02 2022 Thank you! I think my question was misunderstood though.
- WebFreak001 (11/31) Jan 02 2022 as a hack I always do:
- Amit (14/34) Jan 02 2022 Yes! That's what I needed.
- Caten (6/22) Mar 20 2022 Hi, I also need a function to "unquote" string, like this:
- Salih Dincer (11/38) Mar 20 2022 ```d
- cc (13/18) Mar 22 2022 I rolled my own for that recently:
- cc (3/21) Mar 22 2022 Oops, I misread what you were asking for. Was thinking quotes
- JG (11/31) Jan 02 2022 Also a bit of a hack.
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
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
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
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
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
On Sunday, 2 January 2022 at 21:16:55 UTC, Amit wrote:On Sunday, 2 January 2022 at 19:26:50 UTC, WebFreak001 wrote:Hi, I also need a function to "unquote" string, like this: ```d assert(unquote(`\n`)=="\n"); ``` Is there a way to do that?[...]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 ^_^
Mar 20 2022
On Sunday, 20 March 2022 at 09:42:44 UTC, Caten wrote:On Sunday, 2 January 2022 at 21:16:55 UTC, Amit wrote:```d void main() { import std.array : replace; auto q = `Hello,\n deneme`; auto p = "Hello,\n deneme"; assert(q.replace("\\n", "\n") == p); } ``` SDB 79On Sunday, 2 January 2022 at 19:26:50 UTC, WebFreak001 wrote:Hi, I also need a function to "unquote" string, like this: ```d assert(unquote(`\n`)=="\n"); ``` Is there a way to do that?[...]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 ^_^
Mar 20 2022
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
On Tuesday, 22 March 2022 at 07:18:00 UTC, cc wrote:On Sunday, 20 March 2022 at 09:42:44 UTC, Caten wrote:Oops, I misread what you were asking for. Was thinking quotes like quotation marks, not backslashes.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
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