Thread overview
Printing a quoted string
Jan 02, 2022
Amit
Jan 02, 2022
Ali Çehreli
Jan 02, 2022
Amit
Jan 02, 2022
WebFreak001
Jan 02, 2022
Amit
Mar 20, 2022
Caten
Mar 20, 2022
Salih Dincer
Mar 22, 2022
cc
Mar 22, 2022
cc
Jan 02, 2022
JG
January 02, 2022

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. 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
January 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

January 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 :)

January 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. 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:

writeln([s]);

because arrays get serialized like D strings, there will be additional [ and ] though.

Sample output:

["Hello there \"uwu\" ``\x1B[Dabc\n"]
January 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. 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]));

}
January 02, 2022

On Sunday, 2 January 2022 at 19:26:50 UTC, WebFreak001 wrote:

>

as a hack I always do:

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:

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 ^_^

March 20, 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:

>

[...]

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:

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:

assert(unquote(`\n`)=="\n");

Is there a way to do that?

March 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:

>

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:

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:

assert(unquote(`\n`)=="\n");

Is there a way to do that?

void main()
{
  import std.array : replace;

  auto q = `Hello,\n deneme`;
  auto p = "Hello,\n deneme";

  assert(q.replace("\\n", "\n") == p);
}

SDB@79

March 22, 2022

On Sunday, 20 March 2022 at 09:42:44 UTC, Caten wrote:

>

Hi, I also need a function to "unquote" string, like this:

assert(unquote(`\n`)=="\n");

Is there a way to do that?

I rolled my own for that recently:

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;
}
March 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:

>

Hi, I also need a function to "unquote" string, like this:

assert(unquote(`\n`)=="\n");

Is there a way to do that?

I rolled my own for that recently:

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.