C++中string用法
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个字符为字符串str
string replace(int pos, int n, const char* s); //替换从pos开始n个字符为字符串s
rfind和find区别://rfind从右往左查找 find从左往右查找
代码示例:
#include<iostream>
#include<string>
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<iostream>
#include<string>
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<iostream>
#include<string>
using namespace std;
//string字符获取
void test01()
{
string str = "hello";
for(int i=0;i<str.size();i++)
{
cout << str[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<iostream>
#include<string>
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<iostream>
#include<string>
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;
}
总结:灵活的运用求子串功能,可以在实际开发中获取有效的信息
C/C++基础 文章被收录于专栏
本专栏收录C/C++编程语言相关知识