C++ STL string
string 和 char* 区别
char * 是一个指针
string 是一个类,类内部封装了 char* , 管理这个字符串, 是一个 char* 型的容器
特点:string类内部封装了很多成员方法
例如: 查找 find 拷贝 copy 删除 delete 替换 replace 插入 insert
string管理 char* 所分配的内存,不用担心复制越界和取值越界等, 由类内部进行负责
string构造函数
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string构造函数 5 /* 6 string(); //创建一个空的字符串 例如 string str; 7 string(const char* s); //使用字符串s初始化 8 string(const string& str); //使用一个string对象初始化另一个string对象(拷贝构造) 9 string(int n , char c); //使用n个字符c 初始化 10 */ 11 void test() 12 { 13 string s1;//默认构造 14 15 const char* str = "ss"; 16 string s2(str); 17 18 string s3(s2);//拷贝 19 20 string s4(10, 'a'); 21 } 22 23 int main() 24 { 25 test(); 26 system("pause"); 27 return 0; 28 }
string赋值操作
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string赋值操作 5 /* 6 string& operator = (const char* s); //char*类型字符串 赋值给当前的字符串 7 string& operator = (const string &s); //把字符串s赋给当前的字符串 8 string& operator = (char c); //字符赋值给当前的字符串 9 string& assign(const char *s); //把字符串s赋给当前的字符串 10 string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串 11 string& assign(const string &s); //把字符串s赋给当前字符串 12 string& assign(int n, char c); //用n个字符c赋给当前字符串 13 */ 14 void test() 15 { 16 string str1; 17 str1 = "str111"; 18 string str2; 19 str2 = str1; 20 string str3; 21 str3 = 'a'; 22 string str4; 23 str4.assign("str4"); 24 string str5; 25 str5.assign("str55", 2);//st 26 string str6; 27 str6.assign(str5); 28 string str7; 29 str7.assign(7, 'w');//wwwwwww 30 } 31 int main() 32 { 33 test(); 34 system("pause"); 35 return 0; 36 }
string字符串拼接
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string字符串拼接 5 /* 6 string& operator+=(const char* str); // 重载+=操作符 7 string& operator+=(const char c); // 重载+=操作符 8 string& operator+=(const string& str); // 重载+=操作符 9 string& append(const char* s); //把字符串s连接到当前字符串结尾 10 string& append(const char* s,int n); //把字符串s的前n个字符连接到当前字符串结尾 11 string& append(const string &s); //同operator+=(const string& str) 12 string& append(const string &s,int pos, int n);//字符串s中从pos开始的n个字符串连接到字符串结尾 13 */ 14 void test() 15 { 16 string str1 = "我"; 17 str1 += "是xxx"; 18 cout << "str1 = " << str1 << endl;//str1 = 我是xxx 19 str1 += ':'; 20 cout << "str1 = " << str1 << endl;//str1 = 我是xxx: 21 string str2 = "高渐人"; 22 str1 += str2; 23 cout << "str1" << str1 << endl;//str1 = 我是xxx:高渐人 24 string str3 = "I"; 25 str3.append(" Like"); 26 cout << "str3 = " << str3 << endl;//str3 = I Like 27 str3.append("Life abcd", 4); 28 cout << "str3 = " << str3 << endl;//str3 = I LikeLife 29 str3.append(str2); 30 cout << "str3 = " << str3 << endl;//str3 = I LikeLife高渐人 空格一个字符 31 str3.append(str2, 0, 2);//从第0个位置开始 截取两个字符 一个中文两个字符 32 //参数2:从哪个位置开始截图 参数3:截图字符个数 33 cout << "str3 = " << str3 << endl; 34 } 35 int main() 36 { 37 test(); 38 system("pause"); 39 return 0; 40 }
string查找和替换
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string查找和替换 5 /* 6 int find(const string& str,int pos = 0) const;//查找str第一次出现位置,从pos开始查找 7 int find(const char* s, int pos = 0) const; //朝朝s第一次出现位置,从pos开始查找 8 int find(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符第一次位置 9 int find(const char c, int pos = 0) const; //查找字符c第一次出现位置 10 int rfind(const string& str,int pos = npos) const;//查找str最后一次位置,从pos开始查找 11 int rfind(const char* s, int pos = npos) const; //查找s最后一个出现位置,从pos开始查找 12 int rfind(const char* s, int pos, int n) const, //从pos查找s的前n个字符最后一次位置 13 int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置 14 string& replace(int pos, int n, const string& str);//替换从pos开始n个字符为字符串str 15 string& replace(itn pos, int n, const char* s); //替换从pos开始的n个字符为字符串s 16 */ 17 //查找 18 void test1() 19 { 20 string str1 = "abcdefgde"; 21 int pos = str1.find("de"); 22 if (pos == -1) 23 { 24 cout << "未找到字符串" << endl; 25 } 26 else 27 { 28 cout << "找到字符串pos = " << pos << endl;//3返回起始d的下标 若无,则返回 -1 29 } 30 //rfind 31 pos = str1.rfind("de"); 32 cout << "pos2 = " << pos << endl;// 7 rfind从右往左查找 但是下标还是从左到右 33 } 34 //替换 35 void test2() 36 { 37 string str1 = "abcdefg"; 38 str1.replace(1, 3, "1111");//从1号位置起 3个字符(bcd) 替换为 1111 39 cout << "str1 = " << str1 << endl;//a 1111 efg 40 } 41 int main() 42 { 43 //test1(); 44 test2(); 45 system("pause"); 46 return 0; 47 }
string字符串比较
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string查找和替换 5 /* 6 字符串比较是按字符的ASCII码进行对比 7 = 返回 0 8 > 返回 1 9 < 返回 -1 10 int compare(const string& s) const; //与字符串s比较 11 int compare(const char* s) const; //与字符串s比较 12 */ 13 void test1() 14 { 15 string str1 = "hello"; 16 string str2 = "xello"; 17 if (str1.compare(str2) == 0) 18 { 19 cout << "str1 等于 str2" << endl;//主要功能 比较两个字符串是否相等 20 } 21 else if (str1.compare(str2) > 0) 22 { 23 cout << "str1 大于 str2" << endl; 24 } 25 else 26 { 27 cout << "str1 小于 str2" << endl; // ASCII: h < x str1小于str 28 } 29 } 30 31 int main() 32 { 33 test1(); 34 system("pause"); 35 return 0; 36 }
string字符存取
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string字符存取 5 /* 6 char& operator[](int n); //通过[]方式取字符 7 char& at(int n); //通过at方式获取字符 8 */ 9 void test() 10 { 11 string str = "hello"; 12 cout << "str = " << str << endl; 13 for (int i = 0; i < str.size(); i++)//str.size()返回str字符串长度 5 14 { 15 cout << str[i] << " "; 16 } 17 cout << endl; 18 for (int i = 0; i < str.size(); i++) 19 { 20 cout << str.at(i) << " "; 21 } 22 cout << endl; 23 //修改单个字符 24 str[0] = 'x'; 25 cout << "str = " << str << endl;//str = xello 26 str.at(1) = 'x'; 27 cout << "str = " << str << endl;//str = xxllo 28 } 29 30 int main() 31 { 32 test(); 33 system("pause"); 34 return 0; 35 }
string插入和删除
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string插入和删除 5 /* 6 string& insert(int pos,const char* s); //插入字符串 7 string& insert(int pos,const string& str);//插入字符串 8 string& insert(int pos,int n, char c); //在指定位置插入n个字符c 9 string& erase(int pos, int n = npos); //删除从pos开始的n个字符 10 */ 11 void test() 12 { 13 string str = "hello"; 14 //插入 15 str.insert(1, "111"); 16 cout << "str = " << str << endl;//h111ello 17 //删除 18 str.erase(1, 3);//参数1:从那个位置起 参数2:删除几个字符 19 cout << "str = " << str << endl;//hello 20 } 21 22 int main() 23 { 24 test(); 25 system("pause"); 26 return 0; 27 }
string字串获取
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 //string子串获取 5 /* 6 string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串 7 sub:子 8 */ 9 void test() 10 { 11 string str = "abcdef"; 12 string subStr = str.substr(1, 3);//参数1:起始位置 参数2:几个字符 13 cout << "subStr = " << subStr << endl;//bcd 14 //实用操作 15 string email = "zhangsan@sina.com"; 16 //从邮件地址中 获取 用户名信息 17 int pos = email.find("@"); 18 cout << "pos = " << pos << endl; // 8 19 string usrName = email.substr(0, pos); 20 cout << "usrName = " << usrName << endl;//zhangsan 21 } 22 int main() 23 { 24 test(); 25 system("pause"); 26 return 0; 27 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)