字符串

数组

c-风格字符串是以空字符结尾,,其ASCII码为0,标记字符串的结尾,

1 char dog[] = {'d','o','g'};//不是字符串
2 char dog[] = {'d','o','g',''};//是字符串

拼接字符串

有时字符串很长,不能放到一行中。拼接时第二个字符串的第一个字符将紧跟第一个字符串最后一个字符(不考虑)后面。将被第二个字符串的第一个字符取代。

1 cout << "this is a " "big dog.
";
2 cout << "this is a big dog
";
3 cout << "this is a " 
4         "big dog
";
5 
6 //上面三种情况输出是等价的

string

string位于std名称空间中。

必须包含#include <string>。

未被初始化的string对象的长度被自动设置为0。

string对象将根据字符串的长度自动调整自己的大小。


c++11也允许将列表初始化用于c风格字符串和string对象:

1 string data = {"i love you "}

赋值、拼接和附加

1 string str1,str2,str3;
2 str1 = str2 + str3;
3 str1 += str2;

其他形式的字符串字面值

wchar_t a[] = L"we are friends";
char16_t a[] = u"we are friends";
char32_t a[] = U"we are friends";

c++11还支持Unicode字符编码方案TUF-8。根据编码的数字值,字符可能存储为1~4个八位组,使用前缀u8表示这种类型的字符串字面值。


原始raw字符串

字符表示的就是自己, 就表示两个常规字符。

"( )"用作界定符,使用前缀R来标识原始字符串。

1 cout << R"(jim "heh" use 
 instead of endl.)" << endl;
2 //输出的是:jim "heh" use 
 instead of endl.

如果字符串中包含(),原始字符串允许在 " 中间添加其他字符,只要确保 ) " 之间也包含这些字符就行。

cout << R"+_("who are you?\dfn.")+-" << endl;
//输出:"who are you?\dfn."
原文地址:https://www.cnblogs.com/pacino12134/p/10974477.html