什么是原始字符串? [英] What is a raw string?

查看:268
本文介绍了什么是原始字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++ 17草案n4713中遇到了以下代码段:

I came across this code snippet in C++17 draft n4713:

#define R "x"
const char* s = R"y"; // ill-formed raw string, not "x" "y"

什么是原始字符串 ?

What is a "raw string"? What does it do?

推荐答案

原始字符串文字是旨在简化包含诸如引号之类的嵌套字符的字符串文字。和反斜杠,通常具有分隔符和转义序列的含义。例如,它们对于编码HTML之类的文本很有用。例如,对比

Raw string literals are string literals that are designed to make it easier to include nested characters like quotation marks and backslashes that normally have meanings as delimiters and escape sequence starts. They’re useful for, say, encoding text like HTML. For example, contrast

"<a href=\"file\">C:\\Program Files\\</a>"

这是一个常规的字符串文字,

which is a regular string literal, with

R"(<a href="file">C:\Program Files\</a>)"

这是原始字符串文字。在这里,除引号外还使用括号可以使C ++区分嵌套引号和界定字符串本身的引号。

which is a raw string literal. Here, the use of parentheses in addition to quotes allows C++ to distinguish a nested quotation mark from the quotation marks delimiting the string itself.

这篇关于什么是原始字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆