命名空间
变体
操作

std::strchr

来自 cppreference.cn
< cpp‎ | string‎ | byte
定义于头文件 <cstring>
const char* strchr( const char* str, int ch );
      char* strchr(       char* str, int ch );

查找由 str 指向的字节字符串中字符的首次出现。

终止空字符被认为是字符串的一部分,如果搜索 '\0' 也可以被找到。

内容

[编辑] 参数

str - 指向要分析的空终止字节字符串的指针
ch - 要搜索的字符

[编辑] 返回值

指向在 str 中找到的字符的指针;如果未找到该字符,则为空指针。

[编辑] 示例

#include <cstring>
#include <iostream>
 
int main()
{
    const char* str = "Try not. Do, or do not. There is no try.";
    char target = 'T';
    const char* result = str;
 
    while ((result = std::strchr(result, target)) != nullptr)
    {
        std::cout << "Found '" << target
                  << "' starting at '" << result << "'\n";
 
        // Increment result, otherwise we'll find target at the same location
        ++result;
    }
}

输出

Found 'T' starting at 'Try not. Do, or do not. There is no try.'
Found 'T' starting at 'There is no try.'

[编辑] 参见

在一个数组中搜索字符的首次出现
(函数) [编辑]
查找给定子字符串的首次出现
(std::basic_string<CharT,Traits,Allocator> 的公共成员函数) [编辑]
在一个宽字符串中查找宽字符的首次出现
(函数) [编辑]
查找字符的最后一次出现
(函数) [编辑]
查找来自分隔符集合中任何字符的第一个位置
(函数) [编辑]