Re: Printing a quoted string

2022-03-22 Thread cc via Digitalmars-d-learn
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

Re: Printing a quoted string

2022-03-22 Thread cc via Digitalmars-d-learn
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)

Re: Printing a quoted string

2022-03-20 Thread Salih Dincer via Digitalmars-d-learn
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

Re: Printing a quoted string

2022-03-20 Thread Caten via Digitalmars-d-learn
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

Re: Printing a quoted string

2022-01-02 Thread Amit via Digitalmars-d-learn
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

Re: Printing a quoted string

2022-01-02 Thread JG via Digitalmars-d-learn
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

Re: Printing a quoted string

2022-01-02 Thread WebFreak001 via Digitalmars-d-learn
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

Re: Printing a quoted string

2022-01-02 Thread Amit via Digitalmars-d-learn
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

Re: Printing a quoted string

2022-01-02 Thread Ali Çehreli via Digitalmars-d-learn
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

Printing a quoted string

2022-01-02 Thread Amit via Digitalmars-d-learn
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