一、概念
string是C++标准库的一个重要的部分,主要用于字符串处理。本质是一个类。
string和char * 区别:
char * :是一个指针。
string:是一个类,其类内部封装了char * ,管理这个字符串,是一个char * 类型的容器。
二、string的构造函数
string(); 创建一个=空字符串 例:string str; string (const char* s);//使用字符串s初始化 string(const string& str); //使用一个string对象初始化另一个string对象 string(int n, char c); //使用n个字符c初始化
使用:
string s1; //创建空字符串,调用无参构造函数
const char* str = "hello world";
string s2(str); //把c_string转换成了string
string s3(s2); //调用拷贝构造函数
string s4(10, 'a');
三、string字符串的赋值操作
描述:
给string字符串进行赋值操作。
赋值函数原型:
string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串 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 str1;
str1 = "hello world";
string str2;
str2 = str1;
string str3;
str3 = 'a';
string str4;
str4.assign("hello c++");
string str5;
str5.assign("hello c++",5);
string str6;
str6.assign(str5);
string str7;
str7.assign(5, 'x');
四、string字符串的拼接、查找和替换
string字符串拼接:函数原型:
string& operator+=(const char* str); //重载+=操作符
string& operator+=(const char c); //重载+=操作符
string& operator+=(const string& str); //重载+=操作符
string& append(const char *s); //把字符串s连接到当前字符串结尾
string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s); //同operator+=(const string& str)
string& append(const string &s, int pos, int n); //字符串s从pos开始的n个字符连接到字符串结尾
查找和替换:
int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找 int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找 int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置 int find(const char c, int pos = 0) const; //查找字符c第一次出现位置 int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找 int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找 int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置 int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置 string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s
find查找是从左往后,rfind从右往左
find找到字符串后返回查找的第一个字符位置,找不到返回-1
replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串
五、string字符串的比较、存取
比较的方式:是根据字符的ASCII码进程比较 = 返回 0 > 返回 1 < 返回 -1
函数原型:
int compare(const string &s) const; //与字符串s比较 int compare(const char *s) const; //与字符串s比较
例子:
string s1 = "qweqwe"; string s2 = "aaaa"; int ret = s1.compare(s2);
存取:
char& operator[](int n); //通过[]方式取字符 char& at(int n); //通过at方法获取字符
例子:
void test01() { string str = "hello world"; for (int i = 0; i < str.size(); i++) { cout << str[i] << " "; } cout << endl; for (int i = 0; i < str.size(); i++) { cout << str.at(i) << " "; } cout << endl; //字符修改 str[0] = 'x'; str.at(1) = 'x'; cout << str << endl; }
六、string的插入和删除、子串
插入和删除的函数原型:
string& insert(int pos, const char* s); //插入字符串 string& insert(int pos, const string& str); //插入字符串 string& insert(int pos, int n, char c); //在指定位置插入n个字符c string& erase(int pos, int n = npos); //删除从Pos开始的n个字符
例子:
string str = "hello"; str.insert(1, "111"); cout << str << endl; str.erase(1, 3); //从1号位置开始3个字符 cout << str << endl;
插入和删除的起始下标都是从0开始
子串:
获取字符串中想要的子串。
函数原型:
string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
例子:
string str = "abcdefg"; string subStr = str.substr(1, 3); cout << "subStr = " << subStr << endl; string email = "hello@sina.com"; int pos = email.find("@"); string username = email.substr(0, pos); cout << "username: " << username << endl;