C++ String find_first_of()

2024 年 8 月 30 日 | 阅读 2 分钟

该函数用于查找指定字符首次出现的位置。

语法

假设有字符串 str1 和 str。语法如下:

参数

str : 包含要搜索的字符的字符串。

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

n : 标识要搜索的字符的数量。

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

返回值

它返回被搜索到字符的位置。

示例 1

让我们来看一个简单的例子。

输出

String contains : Dancing is my favorite hobby
Position of the first occurrence of the string favorite is 1

示例 2

让我们来看一个指定了开始搜索位置的简单例子。

输出

String contains : Welcome to the programming world
Now, start the search from the second position and we find the first occurrence of the 'programming' is : 4

示例 3

让我们看一个查找单个字符首次出现位置的简单例子。

输出

String contains javatpoint tutorial
Position of the first occurrence of 'a' character is : 1

下一主题C++ 字符串