C++ String Find()
C++ String Find()
此函数用于查找指定的子字符串。
语法
考虑两个字符串str1和str2、语法为:
参数
str: 要搜索的字符串。
pos:
n: 要搜索的字符串中的字符数。
ch : 定义要搜索的字符。
返回值
它返回第一个匹配项的第一个字符的位置。
示例1
让我们看一个简单的示例。
#include<iostream>
using namespace std;
int main()
{
string str= "java is the best programming language";
cout << str<<'\n';
cout <<" Position of the programming word is :";
cout<< str.find("programming");
return 0;
}
输出:
Java is the best programming language
Position of the programming word is 17
示例2
让我们看一个简单的示例,将字符的位置作为参数传递。
#include<iostream>
using namespace std;
int main()
{
string str= "Mango is my favorite fruit";
cout << str<<'\n';
cout<< " position of fruit is :";
cout<< str.find("fruit",12);
return 0;
}
输出:
Mango is my favorite fruit
Position of fruit is 21
示例3
让我们看一下找到单个字符的简单示例。
#include<iostream>
using namespace std;
int main()
{
string str = "lidihuo";
cout << "String contains :" << str;
cout<< "position of p is :" << str.find('p');
return 0;
}
输出:
String contains : lidihuo
Position of p is 5