1.本质:string是C++风格的字符串,而string本质上是一个类
string和char*的区别
char*是一个指针
string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
特点:
string类内部封装了很多的成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
2.string构造函数
构造函数原型:
void test01()
{
string str1;//默认构造
const char* str = "hello,word";
string str2(str);
cout << "str2=" << str2 << endl;
string str3(str2);//拷贝
cout<< "str3=" << str3 << endl;
string str4(10, 'a');//赋值10个a
cout << "str4=" << str4 << endl;
}
3.string赋值操作
void test01()
{
//1
string str1;
str1 = "hellow,world";
cout << "str1=" << str1 << endl;
//2
string str2 = str1;
cout << "str2=" << str2 << endl;
//3
string str3;
str3 = 'c';
cout << "str3=" << str3 << endl;
//4
string str4;
str4.assign("hellow,C++");
cout << "str4=" << str4 << endl;
//5
string str5;
str5.assign("hellow,C++", 5);
cout << "str5=" << str5 << endl;
//6
string str6;
str6.assign(str5);
cout << "str6=" << str6 << endl;
//7
string str7;
str7.assign(10,'w');//赋值10个w
cout << "str7=" << str7 << endl;
}
常用的是operator=
4.字符串拼接
void test01()
{
//字符串拼接
//1
string str1 = "我";
str1 += "爱玩游戏:";
cout << "str1=" << str1 << endl;
//2
str1 += 'c';
cout << "str1=" << str1 << endl;
//3
string str2 = "LOL,火影忍者手游";
str1 += str2;
cout << "str1=" << str1 << endl;
//4
string str3 = "我";
str3.append("爱玩游戏");
cout << "str3=" << str3 << endl;
//5
str3.append("火影忍者手游,LOL", 4);//输出为我爱玩游戏:火影
//一个汉字相当于两个字符占2字节
cout << "str3=" << str3 << endl;
//6
str3.append(str2);
cout << "str3=" << str3 << endl;
//7
str3.append(str2,4, 8);//从str2中的下标为4开始截取,截取8个字符拼接到str3上
cout << "str3=" << str3 << endl;
}
5.string的查找和替换
void test01()//查找
{
string str1 = "abcdefcdg";
int pos = str1.find("cd");//未找到返回-1,找到返回下标
if (pos == -1)
{
cout << "未找到该字符串" << endl;
}
else
{
cout << "找到字符串,下标pos=" << pos << endl;
}
//rfind//从右查找
int rpos = str1.rfind("cd");
if (pos == -1)
{
cout << "未找到该字符串" << endl;
}
else
{
cout << "找到字符串,下标rpos=" << rpos << endl;
}
}
void test02()//替换
{
string str1 = "abcdef";
str1.replace(1, 3, "121212121");//从下标为1的位置开始替换,将3个字符替换为121212121
cout << "str1=" << str1 << endl;
}
6.字符串的比较:逐一比较ASCII码值的大小(主要比较两字符串相等)
str.compare(str2)
等于返回0
大于返回1
小于返回-1
void test01()
{
//字符串比较
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2)==0)//逐一比较ASCII码值的大小
{
cout << "str1==str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1>str2" << endl;
}
else
{
cout << "str1<str2" << endl;
}
}
7.string字符存取(单个字符的读和写)
void test01()
{
//字符串比较
string str1 = "hello";
cout << "str1=" << str1 << endl;
//1通过[]访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] <<" ";
}
cout << endl;
//2通过at方式访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
//修改
str1[0] = 'a';
cout << "str1=" << str1 << endl;
str1.at(0) = 'b';
cout << "str1=" << str1 << endl;
}
8.string插入和删除
void test01()
{
string str1 = "abcde";
//插入
str1.insert(1, "111");
cout << "str1=" << str1 << endl;;//a111bcde
//删除
str1.erase(1, 2);//第几个位置,删除几个字符
cout << "str1=" << str1 << endl;;//a1bcde
}
9.string字串
从字符串中获取想要的子串
void test01()
{
string str1 = "abcde";
string substr = str1.substr(1, 3);//从哪个位置起,截取多少个
cout << "substr=" << substr << endl;
}
void test02()//使用操作
{
string str = "zhangsan@sina.com";
int pos = str.find("@");//返回8
string username = str.substr(0, pos);
cout << username << endl;
}