[C++] STL container - string string operation ⑤ (string string search | find function to find string | rfind function to find string)






1. String character search - find function searches for strings



1. String class find function prototype description


String class find function searches for strings: In addition to finding single characters, the string class find function can also search for substrings. If no string is found, -1 is returned;

  • Start searching for characters from the specified position: In the string string, start from the pos index position (including the position index itself) to find the character c at the position of the current string, if If not found, return -1;
int find(char c,int pos=0) const;
  • Start searching for the char* string at the specified position: In the string string, search for the char* type string s starting from the pos index position (including the position index itself). The position of the current string, if not found, returns -1;
int find(const char *s, int pos=0) const;
  • Search for the string string starting from the specified position: In the string string, search for the string type string s starting from the pos index position (including the position index itself) at the current character The position of the string, if not found, -1 will be returned;
int find(const string &s, int pos=0) const;

2. Code example - string search


Code example:

#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;
};

Results of the :

index: 0
index: 28
Please press any key to continue. . .

Insert image description here


3. Code example - counting string substrings


Find the subscript and number of occurrences of the "Tom" string;

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

First, find the first subscript, and then start the loop;

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

Then, set the loop condition: if string::npos is not found, which is -1, if the return result is not equal to string::npos / -1, the loop will continue until string::npos / -1 is returned. ;

In the loop, the index increases by 3 each time and continues to search for subsequent indexes. The string searched this time is skipped here;

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

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

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

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

Code example:

#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;
};

Results of the :

The index where the Tom string appears index = 0
The index where the Tom string appears = 28
The number of times the Tom string appears count = 2
Please press any key to continue. . .

Insert image description here





2. String character search - rfind function searches for strings



1. String class rfind function prototype description


string The rfind-like function searches for a string: Searches for character c from right to left in the string starting from the specified position; if found, returns the position of the character in the string , the returned position index starts counting from 0; if not found, return string::npos / -1;

  • Search for characters starting at the specified position: In the string string, starting from npos index position (including the The position index itself) starts to search the position of character c in the current string from right to left. If it is not found, it returns -1; if it is found, it returns the position of the character in the string. The returned position index starts counting from 0; If not found returnstring::npos / -1;
int rfind(char c, int pos=npos) const;
  • Search for the char* string starting from the specified position: In the string string, start from the npos index position (Including the position index itself) Start searching from right to leftchar* type string s at the position of the current string, if not found, return -1; if found, return the character in the character The position in the string, the returned position index starts counting from 0; if not found, returnstring::npos / -1;
int rfind(const char *s, int pos=npos) const;
  • Search for the string string starting from the specified position: In the string string, start from the npos index position ( Including the position index itself) start searching from right to leftstring type string s at the position of the current string, if not found, return -1; if found, return the character in the string position in , the returned position index starts counting from 0; if not found, returnstring::npos / -1;
int rfind(const string &s, int pos=npos) const;

2. Code example - rfind string search


Code example:

#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;
};

Results of the :

index: 0
index: 28
Please press any key to continue. . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/135039666