C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ String Find()

C++ String Find()

此函数用于查找指定的子字符串。

语法

考虑两个字符串str1和str2、语法为:
str1.find(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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4