【C++】STL 容器 - string 字符串操作 ⑤ ( string 字符串查找 | find 函数查找字符串 | rfind 函数查找字符串 )






一、string 字符查找 - find 函数查找字符串



1、string 类 find 函数原型说明


string 类 find 函数查找字符串 : string 类的 find 函数除了可以查找单个字符外 , 还可以查找子字符串 , 如果没有查到就返回 -1 ;

  • 从指定位置开始查找 字符 : 在 string 字符串中 , 从 pos 索引位置 ( 包括该位置索引自身 ) 开始查找字符 c 在当前字符串的位置 , 如果没有查到就返回 -1 ;
int find(char c,int pos=0) const;
  • 从指定位置开始查找 char* 字符串 : 在 string 字符串中 , 从 pos 索引位置 ( 包括该位置索引自身 ) 开始查找 char* 类型字符串 s 在当前字符串的位置 , 如果没有查到就返回 -1 ;
int find(const char *s, int pos=0) const;
  • 从指定位置开始查找 string 字符串 : 在 string 字符串中 , 从 pos 索引位置 ( 包括该位置索引自身 ) 开始查找 string 类型字符串 s 在当前字符串的位置 , 如果没有查到就返回 -1 ;
int find(const string &s, int pos=0) const;

2、代码示例 - 字符串查找


代码示例 :

#include "iostream"
using namespace std;
#include "string"

int main() {
    
    

	string s1 = "Tom And Jerry, Hello World, Tom !";

	// 从 0 位置开始 ( 包括 0 位置 ) , 统计第一次出现 Tom 字符串的位置
	int index = s1.find("Tom", 0);
	// index: 0
	cout << "index: " << index << endl;

	// 从 4 位置开始 ( 包括 4 位置 ) , 统计第一次出现 Tom 字符串的位置
	index = s1.find("Tom", 4);
	// index: 28
	cout << "index: " << index << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

index: 0
index: 28
请按任意键继续. . .

在这里插入图片描述


3、代码示例 - 统计字符串子串


查找 “Tom” 字符串出现的 下标 和 次数 ;

	string s1 = "Tom And Jerry, Hello World, Tom !";

首先 , 查找出第一次下标 , 然后开启循环 ;

	// 1. 先查找出第一次下标
	int index = s1.find("Tom", 0);

然后 , 设置循环条件 : 如果没有查到到返回 string::npos 也就是 -1 , 如果查找到了 返回结果不等于 string::npos / -1 就一直循环下去 , 直到返回 string::npos / -1 为止 ;

在循环中 , 每次索引自增 3 , 继续查找后续索引, 此处跳过本次查找的字符串 ;

	while (index != string::npos)
	{
    
    
		cout << "出现 Tom 字符串 的索引 index = " << index << endl;

		// 索引自增, 继续查找后续索引, 此处跳过本次查找的字符串
		index = index + 3;

		// 继续 基于新的索引 向后查找
		index = s1.find("Tom", index);

		// 每次统计 find 结果不为 -1 , count 就自增 1
		count++;
	}

代码示例 :

#include "iostream"
using namespace std;
#include "string"

int main() {
    
    

	string s1 = "Tom And Jerry, Hello World, Tom !";

	//查找 "Tom" 字符串出现的 下标 和 次数
	// 1. 先查找出第一次下标
	int index = s1.find("Tom", 0);

	// 保存出现次数
	int count = 0;

	// 2. 设置循环条件 : 如果没有查到到返回 string::npos 也就是 -1
	//	  如果查找到了 返回结果不等于 string::npos / -1 就一直循环下去
	//	  直到返回 string::npos / -1 为止
	while (index != string::npos)
	{
    
    
		cout << "出现 Tom 字符串 的索引 index = " << index << endl;

		// 索引自增, 继续查找后续索引, 此处跳过本次查找的字符串
		index = index + 3;

		// 继续 基于新的索引 向后查找
		index = s1.find("Tom", index);

		// 每次统计 find 结果不为 -1 , count 就自增 1
		count++;
	}

	cout << "出现 Tom 字符串 的次数 count = " << count << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

出现 Tom 字符串 的索引 index = 0
出现 Tom 字符串 的索引 index = 28
出现 Tom 字符串 的次数 count = 2
请按任意键继续. . .

在这里插入图片描述





二、string 字符查找 - rfind 函数查找字符串



1、string 类 rfind 函数原型说明


string 类 rfind 函数查找字符串 : 在字符串中从 指定位置 开始 从右到左 查找字符 c ; 如果找到 则返回该字符在字符串中的位置 , 返回的位置索引 从0开始计数 ; 如果没有找到返回string::npos / -1 ;

  • 从指定位置开始查找 字符 :string 字符串中 , 从 npos 索引位置 ( 包括该位置索引自身 ) 开始 从右向左 查找字符 c 在当前字符串的位置 , 如果没有查到就返回 -1 ; 如果找到 则返回该字符在字符串中的位置 , 返回的位置索引 从0开始计数 ; 如果没有找到返回string::npos / -1 ;
int rfind(char c, int pos=npos) const;
  • 从指定位置开始查找 char* 字符串 :string 字符串中 , 从 npos 索引位置 ( 包括该位置索引自身 ) 开始 从右向左 查找 char* 类型字符串 s 在当前字符串的位置 , 如果没有查到就返回 -1 ; 如果找到 则返回该字符在字符串中的位置 , 返回的位置索引 从0开始计数 ; 如果没有找到返回string::npos / -1 ;
int rfind(const char *s, int pos=npos) const;
  • 从指定位置开始查找 string 字符串 :string 字符串中 , 从 npos 索引位置 ( 包括该位置索引自身 ) 开始 从右向左 查找 string 类型字符串 s 在当前字符串的位置 , 如果没有查到就返回 -1 ; 如果找到 则返回该字符在字符串中的位置 , 返回的位置索引 从0开始计数 ; 如果没有找到返回string::npos / -1 ;
int rfind(const string &s, int pos=npos) const;

2、代码示例 - rfind 字符串查找


代码示例 :

#include "iostream"
using namespace std;
#include "string"

int main() {
    
    

	string s1 = "Tom And Jerry, Hello World, Tom !";


	// 从 0 位置开始 ( 包括 0 位置 ) , 统计第一次出现 Tom 字符串的位置
	int index = s1.rfind("Tom", 0);
	// index: 0
	cout << "index: " << index << endl;

	// 从 末尾 位置开始 ( 包括 末尾 位置 ) , 统计第一次出现 Tom 字符串的位置
	index = s1.rfind("Tom", s1.length() - 1);
	// index: 28
	cout << "index: " << index << endl;


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

index: 0
index: 28
请按任意键继续. . .

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/135039666