string是一个类,类内部封装了char*,管理这个字符串,为char型容器。
目录
string构造函数:
string赋值操作:
string字符串拼接:
string查找和替换:
string字符串比较:
string字符存取:
string插入和删除:
string子串:
string构造函数:
1.string(); 创建一个空的字符串
2.string(const char *str); 使用字符串str初始化
3.string(const string& str); 使用一个string对象初始化另一个string对象
4.string(int num,char ch); 使用num个字符ch进行初始化
#include#include using namespace std; int main() { string str1; const char *str = "Hello,World!"; string str2(str); string str3(str2); string str4(5,'c'); cout << "str1=" << str1 << endl; cout << "str2=" << str2 << endl; cout << "str3=" << str3 << endl; cout << "str4=" << str4 << endl; return 0; }
运行结果:
str1=
str2=Hello World!
str3=Hello World!
str4=ccccc
string赋值操作:
1.string& operator=(const char *str); 将字符串赋值给当前的字符串
2.string& operator=(const string& str); 将字符串str赋值给当前的字符串
3.string& operator=(char ch); 将字符ch赋值给当前的字符串
4.string& assign(const char *str); 将字符串str赋值给当前的字符串
5.string& assign(const char *str,int num); 将字符串str的前num个字符赋值给当前的字符串
6.string& assign(const string& str); 将字符串str赋值给当前字符串
7.string& assign(int num,char ch); 用num个字符ch赋值给当前字符串
#include#include using namespace std; int main() { string str1,str2,str3,str4,str5,str6,str7; //将字符串"1-Hello,world!"赋值给str1 str1 = "1-Hello,world!"; //将字符串str1赋给str2 str2 = str1; //将字符x赋给str3 str3 = 'x'; //将字符串"2-Hello,world!"赋值给str4 str4.assign("2-Hello,world!"); //将字符串"Hello,world!"的前10个字符赋给str5 str5.assign("Hello,world!",10); // 将字符串str5赋给str6 str6.assign(str5); //将10个字符x赋给str7 str7.assign(10,'x'); cout << "str1=" << str1 << endl; cout << "str2=" << str2 << endl; cout << "str3=" << str3 << endl; cout << "str4=" << str4 << endl; cout << "str5=" << str5 << endl; cout << "str6=" << str6 << endl; cout << "str7=" << str7 << endl; return 0; }
运行结果:
str1=1-Hello,world!
str2=1-Hello,world!
str3=x
str4=2-Hello,world!
str5=Hello,worl
str6=Hello,worl
str7=xxxxxxxxxx
string字符串拼接:
1.string& operator += (const char *str);
2.string& operator += (const char ch);
3.string& operator += (const string& str);
4.string& append(const char *str) ; 将字符串str拼接到该字符串结尾
5.string& append(const char *str,int num); 将字符串str的前num个字符拼接到该字符串结尾
6.string& append(const string& str);
7.string& append(const string& str,int pos,int n);
将字符串str中从pos开始的num个字符连接到该字符串结尾
#include#include using namespace std; int main() { string str1,str2,str3,str4,str5,str6,str7; str1 = "I "; cout << "str1=" << str1 << endl; str1 += "am a student"; cout << "str1=" << str1 << endl; str1 += ':'; cout << "str1=" << str1 << endl; str2 = " a good student"; str1 += str2; cout << "str1=" << str1 << endl; str3 = "You "; str3.append("are a student."); cout << "str3=" << str3 << endl; str3.append("abcdef",3); cout << "str3=" << str3 << endl; str3.append(str2); cout << "str3=" << str3 << endl; str3.append(str2,3,3); cout << "str3=" << str3 << endl; return 0; }
运行结果:
str1=I str1=I am a student str1=I am a student: str1=I am a student: a good student str3=You are a student. str3=You are a student.abc str3=You are a student.abc a good student str3=You are a student.abc a good studentgoo
string查找和替换:
1.int find(const string& str,int pos = 0) const; 从pos开始查找str第一次出现的位置
2.int find(const char *s,int pos = 0) const; 从pos开始查找s第一次出现的位置
3.int find(const char *s,int pos,int n) const; 从pos位置查找s的前n个字符第一次位置
4.int find(const char c,int pos = 0) const; 查找字符c第一次出现的位置
5.int rfind(const string& str,int pos = npos) const; 从pos开始查找str最后一次位置
6.int rfind(const char *s,int pos = npos) const; 从pos开始查找s最后一次位置
7.int rfind(const char *s,int pos,int n) const; 从pos查找s的前n个字符最后一次位置
8.int rfind(const char c,int pos = 0) const; 查找字符c最后一次出现位置
9.string& replace(int pos,int n,const string& str); 替换从pos开始n个字符为字符串str
10.string& replace(int pos,int n,const char *s); 替换从pos开始的n个字符为字符串s
#include#include using namespace std; void Print(int pos) { if(pos == -1)//未找到,输出no cout << "no" << endl; else cout << "pos = " << pos << endl; } int main() { string str = "wstuvwxyzr"; int pos; pos = str.find("xy"); Print(pos); pos = str.find("xy",4); Print(pos); pos = str.find("xy",4,3); Print(pos); pos = str.rfind('w'); Print(pos); pos = str.rfind('w',2);//从2开始查找'w'最后一次位置 Print(pos); str.replace(1,5,"12345"); cout << "str = " << str << endl; return 0; }
运行结果:
pos = 6 pos = 6 no pos = 5 pos = 0 str = w12345xyzr
string字符串比较:
字符串比较是按ASCII码进行比较的
1.int compare(const string& s) const; 与字符串s比较
2.int compare(const char *s) const;
#include#include using namespace std; void Print(int res) { if(!res) cout << "str1 等于 str2" << endl; else if(res > 0) cout << "str1 大于 str2" << endl; else cout << "str1 小于 str2" << endl; } int main() { string str1,str2; str1 = "Hello"; str2 = "World"; int res = str1.compare(str2); cout << "res = " << res << endl; Print(res); str1 = "Hello"; str2 = "Hello"; res = str1.compare(str2); cout << "res = " << res << endl; Print(res); str1 = "World"; str2 = "Hello"; res = str1.compare(str2); cout << "res = " << res << endl; Print(res); return 0; }
运行结果:
res = -15 str1 小于 str2 res = 0 str1 等于 str2 res = 15 str1 大于 str2
string字符存取:
1.char& operator[ ](int n); 通过[ ]方式取字符
2.char& at(int n); 通过at方式获取字符
#include#include using namespace std; int main() { int i; string str = "Hello World!"; for(i = 0;i < str.size();i++) { cout << str[i]; } cout << endl; for(i = 0;i < str.size();i++) { cout << str.at(i); } cout << endl; str[0] = '5'; str.at(1) = '6'; cout << str << endl; return 0; }
运行结果:
Hello World! Hello World! 56llo World!
string插入和删除:
1.string& insert(int pos,const char *s); 插入字符串
2.string& insert(int pos,const string& str); 插入字符串
3.string& insert(int pos,int n,char ch); 在指定位置插入n个字符ch
4.string& erase(int pos,int n = npos); 删除从npos开始的n个字符
#include#include using namespace std; int main() { string str = "Hello World!"; str.insert(1,"xyz"); cout << str << endl; str.insert(2,3,'m'); cout << str << endl; str.erase(1,5); cout << str << endl; //删除"Hxmmmyzello World!"中从x开始的5个字符 return 0; }
运行结果:
Hxyzello World! Hxmmmyzello World! Hzello World!
string子串:
string substr(int pos = 0,int n) const; 返回从pos开始的n个字符组成的字符串
#include#include using namespace std; int main() { string str1 = "Hello World!"; string str2 = str1.substr(1,4); cout << "str2 = " << str2 << endl; return 0; }
运行结果:
str2 = ello



