无涯教程-进程 - int rfind()函数

59 阅读1分钟

此函数用于查找由参数指定的最后一次出现的字符串。

int rfind - 语法

str.rfind(s);
str.rfind(s,pos);
str.rfind(s,pos,n);
str.rfind(ch);

int rfind - 范围

str     -  str是用于搜索的字符串对象。

pos   -  它定义了开始搜索的最后一个字符的位置。

n        -  搜索中要考虑的字符数

ch      -  要搜索的字符值。

int rfind - 例子1

让我们看看这个简单的例子。

#include<iostream>
using namespace std;
int main()
{
	string str="This is an object oriented programming language";
	string key="language";
	int i=str.rfind(key);
	cout<<i;
	return 0;
}

输出:

39

int rfind - 例子2

让我们看一个通过传递字符值的简单例子。

#include<iostream>
using namespace std;
int main()
{
	string str="Computer Science";
            int i=str.rfind(e);
	cout<<i;
	return 0;
}

输出:

15

int rfind - 例子3

让我们看一下在参数中提到位置pos的示例。

#include<iostream>
using namespace std;
int main()
{     
 string str="Digital electronics is a B.tech subject";
int i=str.rfind("is",21);
cout<<i;
return 0;
}

输出:

20

int rfind - 例子4

让我们看一下该示例,其中要通过其参数指定要匹配的字符数。

#include<iostream>
using namespace std;
int main()
{
	string str="Java is an object oriented programming language";
            int i=str.rfind("programming",40,7);
	cout<<i;
	return 0;
}

输出:

27

参考链接

www.learnfk.com/c++/cpp-str…