当要写一个字符串里面有很多斜杠的时候(比如正则表达式),为了便于书写和不出错,可以使用原始字符串字面值常量。
1、std::string的原始字符串字面值常量
#define debug qDebug()<<
#define cppdebug std::cout <<
int main(int argc, char *argv[])
{
std::string s = R"(asdfghjkl\n\t\y\z6666\\\\\)";
cppdebug "cppString " << s;
}
R"(xxx)"的形式表示字符串“xxx”,不会有转义。
当字符里有换行时:
当要在字符串里面包含括号时,在前后加上三个星号:
2、QString中的原始字符串字面值常量
QString的原始字符串字面值常量和std::string的不太一样
如上图,“\”都被加上了一个"\"
如上图,换行则是加上一个“\n”和一段空格,不像std::string的有实际的换行。
字符串中包含括号则与std::string一致:
扫描二维码关注公众号,回复:
12709579 查看本文章