📜  C++ string.rfind()函数

📅  最后修改于: 2020-10-21 02:17:19             🧑  作者: Mango

C++ string.rfind()

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

句法

考虑字符串“ str”和键字符串“ s”。语法为:

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

参数

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

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

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

ch:要搜索的字符值。

例子1

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

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

输出:

39

例子2

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

#include
using namespace std;
int main()
{
    string str="Computer Science";
            int i=str.rfind('e');
    cout<

输出:

15

例子3

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

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

输出:

20

例子4

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

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

输出:

27