目录
一、基本概念
本质
string是c++风格的字符串,而string本质上是一个类
string与char* 区别
char*是一个指针而string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
string特点
string类内部封装了很多成员方法
如,查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部负责
二、string构造函数
构造函数
string(); //创建一个空的字符串,如,string str;
string( const char* s); //使用字符串s初始化
string( const string& str); //使用一个string对象初始化另一个string对象
string( int n, char c); //使用n个字符c初始化
测试
//string构造函数
void Test01() {
string s1;//默认构造
const char* str = "hello c++";
string s2(str);
cout << "s2=" << s2 << endl;//s2=hello c++
string s3(s2);
cout << "s3=" << s3 << endl;//s3=hello c++
string s4(10,'a');
cout << "s4=" << s4 << endl;//aaaaaaaaaa
}
int main() {
Test01();
return 0;
}
三、赋值操作
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赋值操作
void Test02() {
string s1;
s1 = "hello c++";
cout << "s1=" << s1 << endl;//s1=hello c++
string s2;
s2 = s1;
cout << "s2=" << s2 << endl;//s2=hello c++
string s3;
s3 = 'a';
cout << "s3=" << s3 << endl;//s3=a
string s4;
s4.assign("hello c++");
cout << "s4=" << s4 << endl;//s4=hello c++
string s5;
s5.assign("hello c++",2);
cout << "s5=" << s5 << endl;//s5=he
string s6;
s6.assign(s5);
cout << "s6=" << s6 << endl;//s6=he
string s7;
s7.assign(10,'a');
cout << "s7=" << s7 << endl;//s7=aaaaaaaaaa
}
int main() {
Test02();
return 0;
}
string的赋值方式很多,operator=很实用
四、字符串拼接
string& operator+=(const char* s); //重载+=操作符,拼接char*类型字符串
string& operator+=(const string& s); //重载+=操作符,拼接字符串s
string& operator+=(const char c); //重载+=操作符,拼接字符c
string& append(const char *s); //把字符串s连接到当前的字符串结尾
string& append(const char *s,int n); //把字符串s的前n个字符连接到当前的字符串结尾
string& append(const string &s); //同string& operator+=(const string& s);
string& append(const string &s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾
//string字符串拼接
void Test03() {
string s1 = "我";
s1 += "爱冬天";
cout << "s1=" << s1 << endl;//s1=我爱冬天
string s2 = "winter snow";
s1 += s2;
cout << "s1=" << s1 << endl;//s1=我爱冬天winter snow
string s3 = "I";
s3.append("love");
cout << "s3=" << s3 << endl;//s3=Ilove
s3.append("winter abcdef", 6);
cout << "s3=" << s3 << endl;//s3 = Ilovewinter
s3.append(s2);
cout << "s3=" << s3 << endl;//s3 = Ilovewinterwinter snow
s3.append(s2, 0, 6);
cout << "s3=" << s3 << endl;//s3 = Ilovewinterwinter snowwinter
}
int main() {
Test03();
return 0;
}
五、查找和替换
功能描述
查找:查找指定字符串是否存在
替换:在指定位置替换字符串
函数
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,intn) 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
rfind()和find()区别:find()是从左往右查找,而rfind()是从右往左查找
//字符串查找和替换
//1、查找
void Test04() {
//find()
string s1 = "abcdefgfdeabc";
int pos=s1.find("de");
if (pos == -1) {
cout << "未找到字符串" << endl;
}
else {
cout << "成功找到字符串,pos=" << pos << endl;//pos=3
}
//rfind()和find()区别:
//find()是从左往右查找,而rfind()是从右往左查找
pos = s1.rfind("de");
if (pos == -1) {
cout << "未找到字符串" << endl;
}
else {
cout << "成功找到字符串,pos=" << pos << endl;//pos=8
}
}
//2、替换
void Test05() {
string s1 = "abcdefg";
s1.replace(1, 3, "1111");
cout << "s1=" << s1 << endl;//s1=a1111efg
}
int main() {
Test04();
Test05();
return 0;
}
六、字符串比较
功能描述
字符串之间的比较
比较方式
按ASCII码进行比较:相等返回0,大于返回1,小于返回0
函数
int compare(const string& s) const; //与字符串s比较
int compare(const char* s) const; //与字符串s比较
测试
//字符串比较
void Test06() {
string s1 = "hello";
string s2 = "xello";
if (s1.compare(s2) == 0) {
cout << "s1=s2" << endl;
}
else if(s1.compare(s2)>0){
cout << "s1>s2" << endl;
}
else {
cout << "s1<s2" << endl;
}
}
int main() {
Test06();
return 0;
}
一般是用于比较字符串是否相等,大于小于用的比较少
七、字符存取
函数
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方式取字符
测试
//string字符存取
void Test07() {
string s1 = "hello";
//cout << "s1=" << s1 << endl;
//1、通过[]方式取字符
for (int i = 0; i < s1.size(); i++) {
cout << s1[i]<<" ";
}//h e l l o
cout << endl;
//2、通过at方式取字符
for (int i = 0; i < s1.size(); i++) {
cout << s1.at(i) << " ";
}//h e l l o
cout << endl;
//修改单个字符
s1[0] = ' x';
cout << "s1=" << s1 << endl;//s1=xello
s1.at(1)='x';
cout << "s1=" << s1 << endl;//s1=xxllo
}
int main() {
Test07();
return 0;
}
八、插入和删除
功能描述
对string字符串进行插入删除字符操作
函数
string& insert((int pos,const char*s); //插入字符串
string& insert((int pos,const string& s); //插入字符串
string& insert((int pos,int n,char c); //在指定位置插入n个字符c
string& erase((int pos,int n=npos;); //删除从pos开始的n个字符
测试
//字符串的插入和删除
void Test08() {
string s1 = "hello";
//插入
s1.insert(1,"111");
cout << "s1=" << s1 << endl;//s1=h111ello
//删除
s1.erase(1, 3);
cout << "s1=" << s1 << endl;//s1=hello
}
int main() {
Test08();
return 0;
}
九、string子串
功能描述
从字符串中获取想要的子串
这个方法比较实用,要灵活运用
函数
string substr(int pos=0,int n=npos) const; //返回由pos开始的n个字符组成的字符串
测试
//string求子串
void Test09() {
string s1 = "abcdef";
string subStr = s1.substr(1, 3);
cout << "subStr=" << subStr << endl;//subStr=bcd
}
//实用案例
void Test10() {
string email = "zhangsan@sina.com";
//从邮件地址中获取用户名信息
int pos = email.find("@");//查找@的位置
string userName = email.substr(0, pos);//获取@之前的子串
cout << userName << endl;
}
int main() {
Test09();
Test10();
return 0;
}