C++黑马程序员——P189-192. string容器 构造函数,赋值,拼接,查找和替换

  • P189. string容器——构造函数
  • P190. ...——赋值操作
  • P191. ...——字符串拼接
  • P192. ...——字符串查找和替换
  • P189. 构造函数

  

——————————————————————————————————————————————————————————

  

——————————————————————————————————————————————————————————

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 void test01()
 7 {
 8     string s1;  //默认构造
 9 
10     const char* str = "hello world";
11     string s2(str); //第二种构造方法
12     cout << "s2 = " << s2 << endl;
13 
14     string s3(s2);  //拷贝构造
15     cout << "s3 = " << s3 << endl;
16 
17     string s4(10, 'a'); //10个a
18     cout << "s4 = " << s4 << endl;
19 }
20 
21 int main() {
22     test01();
23     return 0;
24 }

res:

  

 

  • P190. 赋值操作

  

——————————————————————————————————————————————————————————

 1 void test01()
 2 {
 3     string str1;
 4     str1 = "hello world";   //char*类型字符串 赋值给string类型字符串
 5     cout << "str1 = " << str1 << endl;
 6 
 7     string str2;
 8     str2 = str1;    
 9     cout << "str2 = " << str2 << endl;
10 
11     string str3;
12     str3 = 'a';     //字符赋值给字符串
13     cout << "str3 = " << str3 << endl;
14 
15     string str4;
16     str4.assign("hello C++");
17     cout << "str4 = " << str4 << endl;
18 
19     string str5;
20     str5.assign("hello C++", 7);    //把字符串的前n个字符赋值给当前字符串
21     cout << "str5 = " << str5 << endl;
22 
23     string str6;
24     str6.assign(str5);
25     cout << "str6 = " << str6 << endl;
26 
27     string str7;
28     str7.assign(10, 'w');   //n个字符 赋值给字符串
29     cout << "str7 = " << str7 << endl;
30 }
31 
32 int main() {
33     test01();
34     return 0;
35 }

res:

  

 

总结:‘=’ 和 assign

 

  • P191. 字符串拼接

  

——————————————————————————————————————————————————————————

 1 void test01()
 2 {
 3     // +=
 4     string str1 = "";
 5     str1 += "爱玩游戏"; //+= char*
 6     str1 += ':';    //+= 字符 
 7 
 8     string str2 = "辐射4";
 9     str1 += str2;   //+= string字符串
10     cout << str1 << endl;
11 
12     // append
13     string str3 = "I";
14     str3.append(" like ");
15     str3.append("game abced", 6);   //字符串的前6个字符
16     cout << str3 << endl;
17     str3.append(str2);
18     str3.append(str2, 0, 2);    //从第0个位置开始的2个字符(C++中一个汉字占2个字符)
19     cout << str3 << endl;
20 }
21 
22 int main() {
23     test01();
24     return 0;
25 }

res:

  

总结: ‘+=’ 和 append

 

  • P192. 字符串查找和替换

  

——————————————————————————————————————————————————————————

 1 //字符串查找和替换
 2 //1. 查找
 3 void test01() {
 4     string str1 = "abcdefgde";
 5     int pos = str1.find("de");
 6     cout << pos << endl;
 7     pos = str1.find("df");
 8     cout << pos << endl;
 9     //rfind 和 find 的区别
10     //rfind是从右往左查找,find是从左往右查找 (返回的都是 查找的字符串的 第一个字符的位置)
11     pos = str1.rfind("de");
12     cout << pos << endl;
13 }
14 
15 //2. 替换
16 void test02() {
17     string str1 = "abcdefg";
18     str1.replace(1, 3, "1111");
19     cout << str1 << endl;
20 }
21 
22 int main() {
23     test01();
24     test02();
25     return 0;
26 }

res:

  

 

  

(〃>_<;〃)(〃>_<;〃)(〃>_<;〃)

 

posted @ 2023-07-07 17:24  我会变强的  阅读(9)  评论(0编辑  收藏  举报