C++ String rfind()
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<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;
}
输出:
示例2
让我们通过传递字符值来看看另一个简单示例。
#include<iostream>
using namespace std;
int main()
{
string str="Computer Science";
int i=str.rfind('e');
cout<<i;
return 0;
}
输出:
示例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;
}
输出:
示例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;
}
输出: