功能描述:
函数原型:
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个字符为字符串strstring& replace(int pos, int n,const char* s);
—————替换从pos开始的n个字符为字符串s代码:
//查找
void test1()
{
string s(20, '-');
string s1 = "ABCDEFGHIJEKLM";
cout << "s1 = " << s1 << endl << s << endl;
cout << "find 查找E" << endl;
int pos = s1.find('E');//find返回值是int //从左边开始查找
if (pos == -1)
{
cout << "未找到" << endl;
}
else
{
cout << "pos = " << pos << endl;//pos是从0开始索引
}
//find与rfind区别
cout << s << endl << "rfind 查找E" << endl;
pos = s1.rfind('E');//rfind返回值是int 从右边开始查找
if (pos == -1)
{
cout << "未找到" << endl;
}
else
{
cout << "pos = " << pos << endl;//pos是从0开始索引
}
cout << s << endl;
}
//替换
void test2()
{
string s1 = "我要坚持减肥。";
//第一个参数是替换起始位置 第二个参数是替换的字符长度 第三个参数是替换内容
//从第8个字符起,4个字符(减肥)被替换成"运动!"
s1.replace(8, 4, "运动!");//中文占两个字符
cout << s1 << endl;
}
总结:
功能描述: 字符串之间的比较
比较方式: 字符串比较是按字符的ASCII码进行对比。
两字符的ASCII码A和B比较:
函数原型:
int compare(const string &s) const;
——与字符串s比较int compare(const char *s) const;
———-与字符串s比较代码:
void printresult(int pos, string s1, string s2)
{
if (pos == 0)
{
cout << s1 <<" = " << s1 << endl;
}
else if (pos == 1)
{
cout << s1 << " > " << s2 << endl;
}
else
{
cout << s1 << " < " << s2 << endl;
}
}
//比较
void test1()
{
cout << "字符串比较:" << endl;
string s1 = "hello";
string s2 = "hello";
string s3 = "hella";
string s4 = "hezl0";
string s(20, '-');
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
cout << endl << "比较s1与s2" << endl;
int pos1 = s1.compare(s2);//逐个字符进行比较 所有字符都相等才返回0
printresult(pos1, s1, s2);
cout << endl << "比较s1与s3" << endl;
int pos2 = s1.compare(s3);//逐个字符进行比较 s1最后一个字符o的ASCII码>s2最后一个字符a的ASCII码
printresult(pos2, s1, s3);
cout << endl << "比较s1与s4" << endl;
int pos3 = s1.compare(s4);//逐个字符进行比较 s1第三个字符l的ASCII码>s2第三个字符z的ASCII码
printresult(pos3, s1, s4);
cout << s << endl;
}
总结:
字符串比较时,是逐个字符进行比较的
字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
string中单个字符存取方式有两种:
char& operator[](int n);
———-通过[]方式取字符char& at(int n);
———————通过at方法获取字符代码:
//存取
void test2()
{
cout << "字符串 取" << endl;
string s = "hello";
cout << "s = " << s << endl;
cout<<"1. 通过[]方式取字符:";
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
cout << "2. 通过at方式取字符:";
for (int i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl << endl;
cout << "字符串 存" << endl;
string s2 = "BelLA";
cout << "s2 = " << s2 << endl;
s2[3] = 'l';
s2.at(4) = 'a';
cout << "s2 = " << s2 << endl;
cout << endl;
}
总结: string字符串中单个字符存取有两种方式,利用[]或 at
功能描述: 对string字符串进行插入和删除字符操作
函数原型:
string& insert(int pos, const char* s);
————插入字符串string& insert(int pos, const string& str);
——插入字符串string& insert(int pos, int n, char c);
————指定位置插入n个字符cstring& erase(int pos, int n = npos);
—————删除从Pos开始的n个字符代码:
//插入和删除
void test1()
{
cout << "字符串插入:" << endl;
string s1 = "Bella";
string s2 = " is";
cout << "s1 = " << s1 << endl;
//insert参数 要插入的起始位置 插入的字符
s1.insert(5, s2);//a的下一个位置插入 Bella is
cout << "s1 = " << s1 << endl;
s1.insert(8, " working out!");//s的下一个位置插入 Bella is working out!
cout << "s1 = " << s1 << endl;
cout << "字符串删除:" << endl;
//insert参数 要插入的起始位置 插入的字符
s1.erase(16, 4);//g的下一个位置删除 Bella is working!
cout << "s1 = " << s1 << endl;
}
功能描述: 从字符串中获取想要的子串
函数原型: string substr(int pos = 0, int n = npos) const;
//返回由pos开始的n个字符组成的字符串
代码:
void test1()
{
cout << "获取子串:" << endl;
string s1 = "Bella is working out!";
string subs1 = s1.substr(0, 5);
cout << "s1 = " << s1 << endl;
cout << "subs1 = " << subs1 << endl << endl;
}
//实用操作
void test2()
{
cout << "实用操作-从邮件地址获取用户名" << endl;
string email = "[email protected]";
cout << "邮箱地址:\t" << email << endl;
//从邮件地址 获取 用户名
int pos = email.find("@");//找到@所在位置
string username = email.substr(0, pos);//从第0个位置开始,截取0-pos长度
cout << "用户名:\t" << username << endl << endl;
}
总结: 灵活的运用求子串功能,可以在实际开发中获取有效的信息