Printing a quoted string
Salih Dincer
salihdb at hotmail.com
Sun Mar 20 10:15:57 UTC 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:
>> 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 at 79
More information about the Digitalmars-d-learn
mailing list