本质:string是C++风格的字符串,本质上是一个类
string和char*的区别:
char*是一个指针string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器特点:
构造函数原型:
string();创建一个空的字符串,如:string str;string(const char* s);使用字符串s初始化string(const char& str);使用一个string对象初始化另一个string对象string(int n,char c);使用n个字符c初始化string的多种构造方式没有可比性,灵活使用即可
//默认构造 string a; //使用字符串初始化 const char* b = "hello"; string c(b); cout << c << endl; //使用一个string对象初始化另一个string对象 string d(c); cout << d << endl; //使用n个字符c初始化 string e(5, 'a'); cout << e << endl;
string赋值操作有很多,operator=这种方式是比较实用的:
string& operator=(const char* s); 字符串赋值给当前的字符串string& operator=(const string &s); 把字符串s赋给当前的字符串string& operator=(char c); 字符赋值给当前的字符串string& assign(const char *s); 把字符串s赋给当前的字符串string& assign(const char *s, int n); 把字符串s的前n个字符赋给当前的字符串string& assign(const string &s); 把字符串s赋给当前字符串string& assign(int n, char c); 用n个字符c赋给当前字符串string a; //字符串赋值给当前的字符串 a = "hello"; cout << a << endl;//hello //把字符串s赋给当前的字符串 string b; b = a; cout << b << endl;//hello //字符赋值给当前的字符串 b = 'h'; cout << b << endl;//h //assign方式 b.assign("world"); cout << b << endl;//world b.assign("world", 1); cout << b << endl;//w b.assign(a); cout << b << endl;//hello b.assign(5, 'a'); cout << b << endl;//aaaaa
实现在字符串末尾拼接字符串
两种方法的函数原型:
string& operator+=(const char* str);string& operator+=(const char c);string& operator+=(const string& str);append()string& append(const char* s);string& append(const char* s, int n);字符串的前n个字符string& append(const string& s);string& append(const string& s, int pos, int n);从第pos个位置开始,截取n个字符string a = "hello"; a.append("world", 2, 2); cout << a << endl;//hellorl
find是从左往右查找:
size_t find(const string& str, int pos = 0) const;//查找str第一次出现位置,从pos开始查找size_t find(const char* s, int pos = 0) const;//查找s第一次出现位置,从pos开始查找size_t find(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符第一次位置size_t find(const char c, int pos = 0) const;//查找字符c第一次出现位置rfind是从右往左查找:
size_t rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找size_t rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找size_t rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置size_t rfind(const char c, int pos = 0) const;//查找字符c最后一次出现位置替换:
string& replace(int pos, int n, const string& str);//替换从pos开始n个字符为字符串strstring& replace(int pos, int n,const char* s);//替换从pos开始的n个字符为字符串sstring a = "hello"; cout << a.find("el") << endl;//1 a.replace(1, 2, "被替换"); cout << a << endl;//h被替换lo cout << a.find("world") << endl;//18446744073709551615 cout << (int)a.rfind("world") << endl;//-1
find()返回值-1与18446744073709551615
find()返回的是size_t,无符号整型,-1在内存中的储存以无符号视角看就是一个很大的数字符串的比较是按字符串的ASCII码进行对比
返回值类型是int,不需要做强制类型转换
返回1
字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
string a = "hello"; cout << a.compare("hell") << endl;//1 cout << a.compare("hello") << endl;//0 cout << a.compare("hellz") << endl;//-1
string中单个字符存取有[]和at两种方式:
char& operator[](int n);char& at(int n);size()获取字符串长度string a = "hello"; //使用size()获取字符串长度 for (int n=0; n < a.size(); n++) { cout << a[n] << endl; }
//修改单个字符 a[0] = 'w'; a.at(1) = 'w'; for (int n = 0; n < a.size(); n++) { cout << a[n] << " "; }
插入字符串:
string& insert(int pos, const char* s);string& insert(int pos, const string* str);在指定位置插入n个字符c:
string& insert(int pos, int n, char c);删除从pos开始的n个字符:
string& erase(int pos, int n =npos);string a = "hello"; a.erase(2); cout << a << endl;//he a = "hello"; a.erase(2, 2); cout << a << endl;//heo
string substr(int pos = 0, int n = npos)const;
string a = "hello"; cout << a.substr(1, 2) << endl;//el cout << a.substr(1) << endl;//ello
从邮箱中提取信息:
string a = "123456@qq.com"; int pos = a.find('@'); cout << "用户名:" << a.substr(0, pos) << endl;//123456 cout << "服务器:" << a.substr(pos + 1) << endl;//qq.com Copyright © 2023 leiyu.cn. All Rights Reserved. 磊宇云计算 版权所有 许可证编号:B1-20233142/B2-20230630 山东磊宇云计算有限公司 鲁ICP备2020045424号
磊宇云计算致力于以最 “绿色节能” 的方式,让每一位上云的客户成为全球绿色节能和降低碳排放的贡献者