C++ 字符串 find_last_of()函数

C++ 字符串 find_last_of()函数

该函数在字符串中搜索与指定字符串中的任何字符匹配的最后一个字符。

语法

假设有两个字符串str1和str2,则语法为:

str1.find_last_of(str2);

参数

str: str是要在其中搜索的字符串。

pos: 它定义了开始搜索的位置。

n: 指定要搜索的字符数。

ch: 它定义了要搜索的字符。

返回值

它返回第一个不匹配的字符的位置。

示例1

让我们看一个简单的示例。

#include<iostream>
using namespace std;
int main()
{
string str = "I love India";
cout<< "String contains :" << str <<'\n';
cout<< str.find_last_of("love");
return 0;
             } 

输出:

String contains : I love India
    5

示例2

让我们看一个简单的示例,当指定查找的起始位置时。

#include<iostream>
           using namespace std;
          int main()
        {
        string str = "C++ Tutorial";
       cout<< "String contains :" << str <<'\n';
      cout<< str.find_last_of("Tutorial",3);
       return 0;
       }   

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程