C++黑马程序员——P193-196. string容器 字符串比较,字符存取,字符串插入和删除,子串获取
- P193. string容器——字符串比较
- P194. ...——字符存取
- P195. ...——字符串插入和删除
- P196. ...——子串获取
- P193. 字符串比较
——————————————————————————————————————————————————————————
1 //字符串比较
2 void test01() {
3 string str1 = "hello";
4 string str2 = "hello";
5 if (str1.compare(str2) == 0) {
6 cout << "str1 == str2" << endl;
7 }
8 }
9
10 int main() {
11 test01();
12 return 0;
13 }
res:
- P194. 字符存取
——————————————————————————————————————————————————————————
1 int main() {
2 string str = "hello";
3
4 for (int i = 0; i < str.size(); i++) {
5 cout << str[i] << " ";
6 }
7 cout << endl;
8
9 for (int i = 0; i < str.size(); i++) {
10 cout << str.at(i) << " ";
11 }
12 cout << endl;
13
14 return 0;
15 }
res:
- P195. 字符串插入和删除
——————————————————————————————————————————————————————————
- P196. 子串获取
——————————————————————————————————————————————————————————
1 int main() {
2 string str = "hello";
3 string str1 = str.substr(1, 3); //str1 = "ell"
4 cout << str1 << endl;
5 return 0;
6 }
res:
(〃>_<;〃)(〃>_<;〃)(〃>_<;〃)