1、substr函数
substr是C++中str::string类的成员函数,用于从字符串中截取子字符串。
基本语法:substr(size_t pos = 0, size_t len = npos)
pos:是子字符串开始的位置(基于 0 的索引)。如果不提供,默认为 0,即从字符串的开头开始。
len:是要提取的子串的长度。如果不提供,npos
(std::string::npos
)将被用作默认值,这意味着子字符串将从 npos
开始一直延伸到原字符串的末尾。
示例:
//1、提取整个字符串:未给任何参数,默认从0开始,到字符串末尾
std::string str = "Hello, World!";
std::string sub = str.substr();
// sub 现在是 "Hello, World!"
//2、从字符串中间开始截取:未给定第二个参数,从中间开始截取到字符串末尾
std::string str1 = "Hello, World!";
std::string sub1 = str1.substr(7); // 从索引 7 开始
// sub 现在是 "World!"
std::string str2 = "Hello, World!";
std::string sub2 = str2.substr(5, std::string::npos);
// sub2 现在是 ", World!"
//3、截取特定长度的子字符串:从索引7开始,截取长度为5的字符串
std::string str3 = "Hello, World!";
std::string sub3 = str3.substr(7, 5); // 从索引 7 开始,长度为 5
// sub3 现在是 "World"
2、字符串查找函数
c++中查找的相关函数有:find()、rfind()、find_first_of()、find_last_of()、find_first_not_of()、find_last_not_of()
1)、find():查找子字符串的第一个出现位置。如果找不到子字符串,返回string::npos。
基本用法:
size_t find(const string& str, size_t pos = 0) const;
size_t find(const char* s, size_t pos = 0) const;
参数说明:
str: 要查找的字符串。
s:要查找的单个字符。
pos:搜索的起始位置(默认为0)
string str = "abcededskegh";
cout << "find():";
cout << str.find("ed") << endl;//在str中查找第一次出现ed的位置 输出结果:3
2)、rfind():逆向查找子字符串的最后一个出现位置。如果找不到子字符串,返回string::npos。
基本用法:
size_t rfind(const string& str, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos = npos) const;
参数说明:
str: 要查找的字符串。
s:要查找的单个字符。
pos:搜索的起始位置(默认从字符串末尾开始查找)
string str = "abcededskegh";
cout << "rfind():";
cout << str.rfind("ed") << endl;//(逆向查找)在str中查找最后一次出现ed的位置 输出结果:5
3)、 find_first_of():正向查找给定字符集合中任意字符第一次出现的位置。如果没有找到,则返回 string::npos 。
基本用法:
size_t find_first_of(charT ch, size_t pos = 0) const;
size_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(const charT* s, size_t pos = 0) const;
size_t find_first_of(const charT* s, size_t pos, size_t n) const;
参数说明:
ch :要查找的单个字符。
s :要查找的字符序列。
pos :开始查找的位置(默认为0)。
str :要查找的字符串。
n :字符序列的长度。
string str = "abcededskegh";
cout << "find_first_of():";
cout << str.find_first_of("edc") << endl;//在str中查找第一个为edc中任意字符的下标 输出结果:2
4)、 find_last_of():反向查找给定字符集合中任意字符最后一次出现的位置。如果没有找到,则返回 string::npos 。
基本用法:
size_t find_last_of (char c, size_t pos = npos) const;
size_t find_last_of (const string& str, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos, size_t n) const;
参数说明:
str :要查找的字符串。
pos:开始查找的位置(默认为npos(字符串的末尾))
s:要查找的字符序列
n :字符序列的长度
c :要搜索的单个字符。
string str = "abcededskegh";
cout << "find_last_of():";
cout << str.find_last_of("cae") << endl;//在str中查找最后一个为cae中任意字符的下标 输出结果:9
5)、 find_first_not_of():正向查找在原字符串中第一个与指定字符串(或字符)中的任一字符都不匹配的字符,返回它的位置。若查找失败,则返回npos。
string str = "abcededskegh";
cout << "find_first_not_of():";
cout << str.find_first_not_of("aec") << endl;//在str中查找第一个不为aec中任意字符的下标 输出结果:1
6)、 find_last_not_of():逆向查找在原字符串中最后一个与指定字符串(或字符)中的任一字符都不匹配的字符,返回它的位置。若查找失败,则返回npos。
string str = "abcededskegh";
cout << "find_last_not_of():";
cout << str.find_last_not_of("lch") << endl;//(逆向查找)在str中查找最后一个不为lch中任意字符的下标 输出结果:10
案例:find和substr结合使用:截取某个字符之前或之后的字符串
string toolC::substring(string s, char* c, bool flag)
{
//字符串c的位置
int index = s.find_first_of(c);//获取第一个字符c的位置
/*获取最后一个字符c的位置*/
// int index = s.find_last_of(c);
//通过flag判断是截取字符c之前的字符串还是之后的字符串
string before;
if (flag)
{
before = s.substr(0,index);
}
else
{
before = s.substr(index + 1);
}
return before;
}
3、replace 函数
std::string 中的 replace 函数用于替换字符串,返回已经被修改的字符串对象
函数原型:
string& replace (size_t pos, size_t len, const string& str);
参数说明:
pos:要替换的子串在原字符串中的起始位置
len:要替换的子串的长度
str:用来替换的字符串
string str1 = "Hello, world!";
str1.replace(6, 6, "C++");
std::cout << str1; // 输出: Hello,C++!
说明:C++中的std::string replace()有多个重载版本,可以满足不同的使用场景。
参考链接: