C++11中的小细节--字符串的原始字面量
2017-07-11 18:12
756 查看
原始字面量很容易理解,即不进行转义的完整字符串。
最近看了看Python,其中讲到了原始字符串。
Both string and bytes literals may optionally be prefixed with a letter ‘r’ or ‘R’; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, ‘\U’ and ‘\u’ escapes in raw strings are not treated specially. Given that python 2.x’s raw unicode literals behave differently than Python 3.x’s the ‘ur’ syntax is not supported.
例如,原始字符串对于反斜杠不会做特殊的处理:
即在Python中,原始字符串以r开头。
这样的功能C++会有吗?
C++11不愧称为modern c++,当然会提供原始字符串了。
但是Python里使用的是r,而c++11中使用的是R。
但是需要注意的是:
原始字符串字面量的定义为:R “xxx(raw string)xxx”
其中,原始字符串必须用括号()括起来,括号的前后可以加其他字符串,所加的字符串会被忽略,并且加的字符串必须在括号两边同时出现。
最近看了看Python,其中讲到了原始字符串。
Both string and bytes literals may optionally be prefixed with a letter ‘r’ or ‘R’; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, ‘\U’ and ‘\u’ escapes in raw strings are not treated specially. Given that python 2.x’s raw unicode literals behave differently than Python 3.x’s the ‘ur’ syntax is not supported.
例如,原始字符串对于反斜杠不会做特殊的处理:
# Python 程序 print r'C:\nowhere'
即在Python中,原始字符串以r开头。
这样的功能C++会有吗?
C++11不愧称为modern c++,当然会提供原始字符串了。
但是Python里使用的是r,而c++11中使用的是R。
但是需要注意的是:
原始字符串字面量的定义为:R “xxx(raw string)xxx”
其中,原始字符串必须用括号()括起来,括号的前后可以加其他字符串,所加的字符串会被忽略,并且加的字符串必须在括号两边同时出现。
#include <iostream> #include <string> int main() { // 一个普通的字符串,'\n'被当作是转义字符,表示一个换行符。 std::string normal_str = "First line.\nSecond line.\nEnd of message.\n"; // 一个raw string,'\'不会被转义处理。因此,"\n"表示两个字符:字符反斜杠 和 字母n。 std::string raw_str = R"(First line.\nSecond line.\nEnd of message.\n)"; std::cout << normal_str << std::endl; std::cout << raw_str << std::endl; std::cout << R"foo(Hello, world!)foo" << std::endl; // raw string可以跨越多行,其中的空白和换行符都属于字符串的一部分。 std::cout <<R"( Hello, world! )" << std::endl; return 0; }
相关文章推荐
- C++11中的小细节--字符串的原始字面量
- C++11中的小细节--字符串的原始字面量
- 拆箱、字面量、字符串+等细节问题
- C++11中的原始字符串
- python 3.3.3 字面量,正则,反斜杠和原始字符串
- C++11 原始字符串
- stm32发字符串的小细节
- 关于c的字符串的一些小细节以及c在计算机里数组的内存存放
- 字符串、包装类、原始数据类型间的转换简单示例
- php中iconv可将字符串从原始编码转化为指定的编码格式
- 字符串处理-研发积累细节
- 原始字符串
- Java 字符串处理的一些小细节
- OGRE1.7.4显示中文字符串细节问题
- 程序猿之---C语言细节14(字符串数组内存分配细节‘\0’空字符、字符串数组赋值细节、字符串换行连接和下标)
- python 入门之 长字符串、原始字符串和unicode
- C 和 C++ 字符串字面量的比较
- c++11(2):字符串、向量和数组
- C/C++字符串字面量的类型
- TypeScript 字符串字面量类型使用出错一例