string类的find()函数总结

本文详细介绍了C++标准库中string类提供的多种字符串搜索函数,包括find(), rfind(), find_first_of(), find_last_of(), find_first_not_of()和find_last_not_of()等函数的不同用法及应用场景。

string类的头文件提供了很多搜索相关的函数比如find()函数及其变体。这使得我们可以以多种不同的方式在字符串中搜索给定的子字符串或字符。但是对于初学者来讲,经常被这些长相类似的函数所混淆。
下面总结了string类的find相关函数:

1、find():

find函数有四种变体:

方法原型描述
size_type find(const string & str, size_type pos = 0) const从字符串的pos位置开始,查找子字符串str。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0) const从字符串的pos位置开始,查找子字符串s。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0, size_type n) const从字符串的pos位置开始,查找s的前n个字符组成的子字符串。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char ch, size_type pos = 0) const从字符串的pos位置开始,查找字符ch。如果找到,则返回该子字符串首次出现的位置;否则,返回string::npos

P.S.string::npos是字符串可储存的最大字符数,通常是无符号int或无符号long的最大取值。

string库还提供了相关的方法:rfind(),find_first_of(),find_last_of(),find_first_not_of(),find_last_not_of()。他们的重载函数特征标都与find()方法相同。

2、rfind():

原型:

size_type rfind(const string & str, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos, size_type n) const;
size_type rfind(const char ch, size_type pos = npos) const;

rfind()方法查找子字符串或字符最后一次出现的位置。

3、find_first_of():

原型:

size_type find_first_of(const string & str, size_type pos = 0) const;
size_type find_first_of(const char * s, size_type pos, size_type n) const;
size_type find_first_of(const char * s, size_type pos = 0) const;
size_type find_first_of(char c, size_type pos = 0) const;

find_first_of()方法在字符串中查找参数中任何一个字符首次出现的位置。例如,下面的语句返回r在”cobra”中的位置(即索引3),因为这个”hark”中各个字母在”cobra”首次出现的位置:

string snake1 = "cobra";
int where = snake1.find_first_of("hark");

4、find_last_of():

原型:

size_type find_last_of(const string & str, size_type pos = npos) const;
size_type find_last_of(const char * s, size_type pos, size_type n) const;
size_type find_last_of(const char * s, size_type pos = npos) const;
size_type find_last_of(char c, size_type pos = npos) const;

find_last_of()方法在字符串中查找参数中任何一个字符最后一次出现的位置。

5、find_first_not_of():

原型:

size_type find_first_not_of(const string & str, size_type pos = 0) const;
size_type find_first_not_of(const char * s, size_type pos, size_type n) const;
size_type find_first_not_of(const char * s, size_type pos = 0) const;
size_type find_first_not_of(char c, size_type pos = 0) const;

find_first_not_of()方法在字符串中查找第一个不包含在参数中的字符,因此下面的语句返回c在”cobra”中的位置,因为”hark”中没有c:

string snake1 = "cobra";
int where = snake1.find_first_not_of("hark");

6、find_last_not_of():

原型:

size_type find_last_not_of(const string & str, size_type pos = npos) const;
size_type find_last_not_of(const char * s, size_type pos, size_type n) const;
size_type find_last_not_of(const char * s, size_type pos = npos) const;
size_type find_last_not_of(char c, size_type pos = npos) const;

find_last_not_of()方法在字符串中查找最后一个不包含在参数中的字符。

### string find 字符串查找方法实现 在 C++ 中,`std::string` 提供了多种字符串查找方法,其中最常用的是 `find` 函数。以下是关于 `find` 函数的详细说明及其实现方式。 #### 1. `find` 函数原型说明 `find` 函数用于在字符串中查找子字符串或字符的位置。其主要函数原型如下: - `size_t find(const string& str, size_t pos = 0) const;` - `size_t find(const char* s, size_t pos = 0) const;` - `size_t find(char c, size_t pos = 0) const;` 上述函数返回第一个匹配项的位置(从索引 `pos` 开始查找),如果未找到,则返回 `std::string::npos`[^2]。 #### 2. `find` 函数的基本实现逻辑 `find` 函数的核心思想是从指定位置开始逐个比较字符串中的字符,直到找到匹配项或遍历完整个字符串。以下是其实现逻辑的伪代码: ```plaintext for (i = pos; i <= str.length() - sub.length(); ++i) { if (str.substr(i, sub.length()) == sub) { return i; } } return std::string::npos; ``` #### 3. 示例代码:使用 `find` 查找子字符串 以下是一个完整的示例,展示如何使用 `find` 函数查找子字符串并输出其位置: ```cpp #include <iostream> #include <string> void searchString(const std::string& str, const std::string& sub) { size_t pos = str.find(sub); // 查找子字符串 if (pos != std::string::npos) { // 判断是否找到 std::cout << "子字符串 \"" << sub << "\" 在位置 " << pos << " 处找到。" << std::endl; } else { std::cout << "子字符串 \"" << sub << "\" 未找到。" << std::endl; } } int main() { std::string str = "abefcdgcdfghcd"; std::string sub = "cd"; searchString(str, sub); return 0; } ``` 运行结果: ```plaintext 子字符串 "cd" 在位置 4 处找到。 ``` #### 4. 多次查找所有匹配项 如果需要查找所有匹配项的位置,可以通过循环调用 `find` 函数,并将起始位置逐步向后移动。以下是一个示例代码: ```cpp #include <iostream> #include <string> void findAllOccurrences(const std::string& str, const std::string& sub) { size_t pos = str.find(sub); // 第一次查找 while (pos != std::string::npos) { // 循环查找所有匹配项 std::cout << "子字符串 \"" << sub << "\" 在位置 " << pos << " 处找到。" << std::endl; pos = str.find(sub, pos + 1); // 继续从下一个位置查找 } } int main() { std::string str = "abefcdgcdfghcd"; std::string sub = "cd"; findAllOccurrences(str, sub); return 0; } ``` 运行结果: ```plaintext 子字符串 "cd" 在位置 4 处找到。 子字符串 "cd" 在位置 9 处找到。 子字符串 "cd" 在位置 13 处找到。 ``` #### 5. 替换查找到的子字符串 如果需要替换查找到的子字符串,可以结合 `find` 和 `replace` 函数实现。以下是一个示例代码: ```cpp #include <iostream> #include <string> void replaceString(std::string& str, const std::string& sub, const std::string& replacement) { size_t pos = str.find(sub); // 查找子字符串 while (pos != std::string::npos) { // 循环替换所有匹配项 str.replace(pos, sub.length(), replacement); // 替换子字符串 pos = str.find(sub, pos + replacement.length()); // 继续查找 } } int main() { std::string str = "abefcdgcdfghcd"; std::string sub = "cd"; std::string replacement = "XX"; replaceString(str, sub, replacement); std::cout << "替换后的字符串: " << str << std::endl; return 0; } ``` 运行结果: ```plaintext 替换后的字符串: abefXXgcdfghXX ``` ### 注意事项 - 如果需要从字符串末尾向前查找,可以使用 `rfind` 函数[^1]。 - `find` 函数返回的位置是基于零索引的,即字符串的第一个字符位置为 0。 - 如果未找到匹配项,`find` 函数会返回 `std::string::npos`,这是一个特殊的值,表示未找到匹配项[^4]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值