string 查找和替换功能描述:
查找:查找指定字符是否存在替换:在指定的位置替换字符串函数原型:
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最后⼀Vic弧线的位置
string replace(int pos,int n,const string& str); //替换从pos开始n个字符为字符串strstring replace(int pos, int n, const char* s); //替换从pos开始n个字符为字符串s rfind和find区别://rfind从右往左查找 find从左往右查找
代码⽰例:
#include using namespace std;//字符串查找和替换//1.查找 void test01(){ string str1 = \"abcdefgde\"; int pos = str1.find(\"de\"); if (pos == -1) { cout << \"未找到字符串\" << endl; } else { cout << \"找到字符串,pos=\" << pos << endl; } //rfind和find区别://rfind从右往左查找 find从左往右查找 pos=str1.rfind(\"de\"); cout << \"pos=\" << pos << endl;} //2.替换 void test02(){ string str1 = \"abcdefg\"; //从1号位置起3个字符替换为1111 str1.replace(1,3,\"1111\"); cout << \"str1=\" << str1 << endl;} int main(){ test01(); test02(); return 0;} 总结: find查找是从左往后,rfind从右往左 find找到字符串后返回查找的⼀个字符位置,找不到返回-1 replace在替换时,要指定从那个位置起,多少个字符,替换成什么样⼦的字符串 string字符串⽐较 功能描述:* 字符串⽐较是按字节的ASCII码进⾏对⽐=返回0 返回1<返回-1 函数原型: int compare(const string &s)const;//与字符串s⽐较int compare(const char *s)const; //与字符串s⽐较代码案例: #include using namespace std;//字符串⽐较void test01(){ string str1 = \"xello\"; string str2 = \"hello\"; if (str1.compare(str2) == 0) { cout << \"str1等于str2\" << endl; } else if (str1.compare(str2) > 0) { cout << \"str1⼤于str2\" << endl; } else { cout << \"str1⼩于str2\" << endl; }} int main(){ test01(); return 0;} 总结:字符串对⽐主要⽤于⽐较两个字符串是否相等,判断谁⼤谁⼩的意义并不是很⼤ string字符串存取 string中单个字符串存取⽅式有两种:char& operator[](int n); //通过[]⽅式取字符char& at(int n); //通过at⽅式获取字符 代码⽰例: #include using namespace std;//string字符获取void test01(){ string str = \"hello\"; for(int i=0;i cout << endl; //2.通过at⽅式访问单个字符 for (int i = 0; i < str.size(); i++) { cout << str.at(i) << \" \"; } cout << endl; //修改单个字符 str[0] = 'x'; //xello cout << \"str=\" << str << endl; str.at(1) = 'x'; //xxllo cout << \"str=\" << str << endl;} int main(){ test01(); return 0;} string插⼊和删除 功能描述: 对string字符串进⾏插⼊和删除字符操作(pos代表位置)函数原型: 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个字符 代码案例: #include using namespace std;//字符串插⼊和删除void test01(){ string str = \"hello\"; //插⼊ str.insert(1, \"111\"); cout << \"str=\" << str << endl; //删除 str.erase(1, 3); cout << \"str=\" << str << endl;} int main(){ test01(); return 0;} 总结:插⼊和删除的起始下标都是从0开始。 string⼦串 功能描述:* 从字符串获取想要的⼦串函数原型: string substr(int pos=0,int n=npos)const; //返回由pos开始的n个字符组成的字符组成的字符串 代码⽰例: #include using namespace std;//string 求⼦串void test01(){ string str = \"abcdef\"; string subStr = str.substr(1, 3); cout << \"subStr=\" << subStr << endl;} //实⽤操作void test02(){ string email = \"lisi@sina.com\"; //从邮件地址中获取⽤户信息 int pos = email.find(\"@\"); cout << pos << endl; string usrName = email.substr(0, pos); cout << usrName << endl;} int main(){ test01(); test02(); return 0;} 总结:灵活的运⽤求⼦串功能,可以在实际开发中获取有效的信息 因篇幅问题不能全部显示,请点此查看更多更全内容