将字符串转换为原始字符串

发布于 2025-02-13 14:27:41 字数 286 浏览 0 评论 0原文

char str[] = "C:\Windows\system32"

auto raw_string = convert_to_raw(str);

std::cout << raw_string;

所需的输出:

C:\Windows\system32

有可能吗?我并不是用额外的后挡板使我的小路弦混乱的忠实拥护者。我也不喜欢显式r“()”符号。

字面上有其他阅读字符串后斜线的工作吗?

char str[] = "C:\Windows\system32"

auto raw_string = convert_to_raw(str);

std::cout << raw_string;

Desired output:

C:\Windows\system32

Is it possible? I am not a big fan of cluttering my path strings with extra backslash. Nor do I like an explicit R"()" notation.

Any other work-around of reading a backslash in a string literally?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2025-02-20 14:27:41

这是不可能的,\在非拉瓦字符串文字中具有特殊的含义,而原始的字符串文字精确存在,以使您有机会避免逃脱东西。放弃,您需要的是r“(...)”

确实,当您写出

char const * str{"a\nb"};

时,您可以验证自己strlen(str) is 3,不是4 ,这意味着一旦您编译了该行,在二进制/对象文件中,只有一个字符,newline字符,对应于\ n;没有\ nor n中没有任何内容,因此您无法检索它们。


作为个人品味,我发现原始的弦乐文字很棒!您甚至可以将真实的输入在其中。通常仅以3个字符的价格 - r和) - 加上您无论如何都会写的那些。好吧,您必须编写更多字符才能避免任何需求逃脱。

查看

std::string s{R"(Hello
world!
This
is
Me!)"};

r到最后)的28个击键,您可以在瞥见中看到它是6行。

等效的非RAW字符串

std::string s{"Hello\nworld!\nThis\nis\nMe!"};

是30个键击,从> code>。 r to last 随附,您必须仔细分析以计数行。

一个很短的字符串,您已经看到了优势。

That's not possible, \ has special meaning inside a non-raw string literal, and raw string literals exist precisely to give you a chance to avoid having to escape stuff. Give up, what you need is R"(...)".

Indeed, when you write something like

char const * str{"a\nb"};

you can verify yourself that strlen(str) is 3, not 4, which means that once you compile that line, in the binary/object file there's only one single character, the newline character, corresponding to \n; there's no \ nor n anywere in it, so there's no way you can retrieve them.


As a personal taste, I find raw string literals great! You can even put real Enter in there. Often just for the price of 3 characters - R, (, and ) - in addtion to those you would write anyway. Well, you would have to write more characters to escape anything needs escaping.

Look at

std::string s{R"(Hello
world!
This
is
Me!)"};

That's 28 keystrokes from R to last " included, and you can see in a glimpse it's 6 lines.

The equivalent non-raw string

std::string s{"Hello\nworld!\nThis\nis\nMe!"};

is 30 keystrokes from R to last " included, and you have to parse it carefully to count the lines.

A pretty short string, and you already see the advantage.

巴黎夜雨 2025-02-20 14:27:41

为了回答问题,不可能是不可能的。

作为不可能的示例,假设我们的路径指定为“ c:\ a \ b”;

现在,str实际上在内存中(在您的程序运行时在程序中)使用静态分配的五个字符的数组,该数组{'c',':':',',','\ 007',' \ 010','\ 000'}其中'\ xyz'代表八分代表示(so '\ 010'是一个char在十进制中等于8等于)。

问题在于,使用字符串字面的五个字符制作五个字符的数组有多种方法。

char str[] = "C:\a\b";
char str1[] = "C:\007\010";
char str2[] = "C:\a\010";
char str3[] = "C:\007\b";
char str4[] = "C:\x07\x08";    // \xmn uses hex coding

在上面,str1str2str3,str4均使用五个char 。

这意味着convert_to_raw(“ c:\ a \ b”)可以合理地假设它通过了上面的任何字符串,并且

std::cout << convert_to_raw("C:\a\b") << '\n';

可以合法地产生输出

C:\007\010

(或其他许多字符串中的任何一个) 。

如果您正在使用Windows路径,则实际问题是c:\ a \ bc:\ 007 \ 010c:\ A \ 010c:\ 007 \ bc:\ x07 \ x08都是Windows下的有效文件名 - (除非它们是硬链接或连接)命名不同的文件。

最后,如果您想在代表文件名或路径的代码中具有字符串文字,则在需要单个后斜线时使用\\或RAW String Liralal。或者,由于Windows API功能也接受这些,因此使用所有正向斜线(例如“ c:/a/b”)将路径作为字符串文字写入代码中。

To answer the question, as asked, no it is not possible.

As an example of the impossibility, assume we have a path specified as "C:\a\b";

Now, str is actually represented in memory (in your program when running) using a statically allocated array of five characters with values {'C', ':', '\007', '\010', '\000'} where '\xyz' represents an OCTAL representation (so '\010' is a char equal to numerically to 8 in decimal).

The problem is that there is more than one way to produce that array of five characters using a string literal.

char str[] = "C:\a\b";
char str1[] = "C:\007\010";
char str2[] = "C:\a\010";
char str3[] = "C:\007\b";
char str4[] = "C:\x07\x08";    // \xmn uses hex coding

In the above, str1, str2, str3, and str4 are all initialised using equivalent arrays of five char.

That means convert_to_raw("C:\a\b") could quite legitimately assume it is passed ANY of the strings above AND

std::cout << convert_to_raw("C:\a\b") << '\n';

could quite legitimately produce output of

C:\007\010

(or any one of a number of other strings).

The practical problem with this, if you are working with windows paths, is that c:\a\b, C:\007\010, C:\a\010, C:\007\b, and C:\x07\x08 are all valid filenames under windows - that (unless they are hard links or junctions) name DIFFERENT files.

In the end, if you want to have string literals in your code representing filenames or paths, then use \\ or a raw string literal when you need a single backslash. Alternatively, write your paths as string literals in your code using all forward slashes (e.g. "C:/a/b") since windows API functions accept those too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文