String类是不可变(final)的,对String类的任何改变,都是返回一个新的String类对象.这样的话把String类的引用传递给一个方法,该方法对String的任何改变,对原引用指向的对象没有任何影响,这一点和基本数据类型相似。
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 // 初始化
6 void test01()
7 {
8 string s1; // 调用无参构造 创建一个空的字符串
9 string s2(10, 'c'); // 使用 n 个字符 c 初始化
10 string s3("abcdefg"); // 使用字符串初始化
11 string s4(s3); // 使用一个 string 对象初始化另一个 string 对象(拷贝构造)
12 cout << s1 << endl;
13 cout << s2 << endl;
14 cout << s3 << endl;
15 cout << s4 << endl;
16 cout << "-----------------" << endl;
17 }
18
19 // 赋值操作
20 // string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
21 // string& operator=(const string &s);//把字符串 s 赋给当前的字符串
22 // string& operator=(char c);//字符赋值给当前的字符串
23 // string& assign(const char *s);//把字符串 s 赋给当前的字符串
24 // string& assign(const char *s, int n);//把字符串 s 的前 n 个字符赋给当前的字符串
25 // string& assign(const string &s);//把字符串 s 赋给当前字符串
26 // string& assign(int n, char c);//用 n 个字符 c 赋给当前字符串
27 // string& assign(const string &s, int start, int n);//将 s 从 start 开始 n 个字符赋值给字符串
28 void test02()
29 {
30 string s1;
31 string s2("appp");
32 s1 = "abcdef";
33 cout << s1 << endl;
34 s1 = s2;
35 cout << s1 << endl;
36 s1 = 'a';
37 cout << s1 << endl;
38 // 成员方法assign
39 s1.assign("jkl");
40 cout << s1 << endl;
41 cout << "-----------------" << endl;
42
43 }
44
45 // 取值操作
46 void test03()
47 {
48 string s1 = "abcdefg";
49 // 重载[]操作符
50 for (int i = 0; i < s1.size(); i++)
51 {
52 cout << s1[i] << " ";
53 }
54 cout << endl;
55 // at成员函数
56 for (int i = 0; i < s1.size(); i++)
57 {
58 cout << s1.at(i) << " ";
59 }
60 cout << endl;
61 try
62 {
63 // cout << s1[100] << endl;
64 cout << s1.at(100) << endl;
65 }
66 catch (...)
67 {
68 cout << "越界" << endl;
69 }
70 cout << "-----------------" << endl;
71 // 区别:[]方式如果访问越界,就直接挂了
72 // at方式 访问越界会抛出异常out_of_range
73 }
74
75 // 拼接操作
76 // string& operator+=(const string& str);//重载+=操作符
77 // string& operator+=(const char* str);//重载+=操作符
78 // string& operator+=(const char c);//重载+=操作符
79 // string& append(const char *s);//把字符串 s 连接到当前字符串结尾
80 // string& append(const char *s, int n);//把字符串 s 的前 n 个字符连接到当前字符串结尾
81 // string& append(const string &s);//同 operator+=()
82 // string& append(const string &s, int pos, int n);//把字符串 s 中从 pos 开始的 n 个字符连接到当前字符串结尾
83 // string& append(int n, char c);//在当前字符串结尾添加 n 个字符 c
84 void test04()
85 {
86 string s = "abcd";
87 string s2 = "1111";
88 s += "abcd";
89 s += s2;
90 cout << s << endl;
91 string s3 = "2222";
92 s2.append(s3);
93 cout << s2 << endl;
94 string s4 = s2 + s3;
95 cout << s4 << endl;
96 cout << "-----------------" << endl;
97 }
98
99 // 查找操作
100 // int find(const string& str, int pos = 0) const; //查找 str 第一次出现位置,从 pos 开始查找
101 // int find(const char* s, int pos = 0) const; //查找 s 第一次出现位置,从 pos 开始查找
102 // int find(const char* s, int pos, int n) const; //从 pos 位置查找 s 的前 n 个字符第一次位置
103 // int find(const char c, int pos = 0) const; //查找字符 c 第一次出现位置
104 // int rfind(const string& str, int pos = npos) const;//查找 str 最后一次位置,从 pos 开始查找
105 // int rfind(const char* s, int pos = npos) const;//查找 s 最后一次出现位置,从 pos 开始查找
106 // int rfind(const char* s, int pos, int n) const;//从 pos 查找 s 的前 n 个字符最后一次位置
107 // int rfind(const char c, int pos = 0) const; //查找字符 c 最后一次出现位置
108 void test05()
109 {
110 string s = "abcdefgffffghijkl";
111 // 查找第一次出现的位置
112 int pos = s.find("fg");
113 cout << "pos: " << pos << endl;
114 // 查找最后一次出现的位置
115 pos = s.rfind("fg");
116 cout << "pos: " << pos << endl;
117 cout << "-----------------" << endl;
118 }
119
120 // string替换
121 // string& replace(int pos, int n, const string& str); //替换从 pos 开始 n 个字符为字符串 str
122 // string& replace(int pos, int n, const char* s); //替换从 pos 开始的 n 个字符为字符串 s
123 void test06()
124 {
125 string s = "abcdefg";
126 s.replace(0, 2, "111");
127 cout << s << endl;
128 cout << "-----------------" << endl;
129 }
130
131 // string 比较
132 /* compare 函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的 A 比小写的 a 小。 */
133 // int compare(const string &s) const; //与字符串 s 比较
134 // int compare(const char *s) const;//与字符串 s 比较
135 void test07()
136 {
137 string s1 = "abcd";
138 string s2 = "abce";
139 if (s1.compare(s2) == 0)
140 {
141 cout << "字符串相等!" << endl;
142 }
143 else
144 {
145 cout << "字符串不相等!" << endl;
146 }
147 cout << "-----------------" << endl;
148 }
149
150 // 子串操作
151 // string substr(int pos = 0, int n = npos) const;//返回由 pos 开始的 n 个字符组成的字符串
152 void test08()
153 {
154 string s = "abcdefg";
155 string mySubstr = s.substr(1, 3);
156 cout << mySubstr << endl;
157 cout << "-----------------" << endl;
158 }
159
160 // 插入和删除
161 // string& insert(int pos, const char* s); //插入字符串
162 // string& insert(int pos, const string& str); //插入字符串
163 // string& insert(int pos, int n, char c);//在指定位置插入 n 个字符c
164 // string& erase(int pos, int n = npos);//删除从 Pos 开始的 n 个字符
165 void test09()
166 {
167 string s = "abcdefg";
168 s.insert(3, "111");
169 cout << s << endl;
170 s.erase(0, 2);
171 cout << s << endl;
172 }
173
174 int main()
175 {
176 test01();
177 test02();
178 test03();
179 test04();
180 test05();
181 test06();
182 test07();
183 test08();
184 test09();
185 getchar();
186 return 0;
187 }
1 //string 转 char*
2 string str = "itcast";
3 const char* cstr = str.c_str();
4 //char* 转 string
5 char* s = "itcast";
6 string sstr(s);