std:: strchr
From cppreference.net
|
定义于头文件
<cstring>
|
||
|
const
char
*
strchr
(
const
char
*
str,
int
ch
)
;
|
||
|
char
*
strchr
(
char
*
str,
int
ch
)
;
|
||
在由 str 指向的字节字符串中查找字符 static_cast < char > ( ch ) 的首次出现位置。
终止空字符被视为字符串的一部分,可通过搜索 ' \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"; // 递增result,否则会在同一位置重复找到目标字符 ++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>
的公开成员函数)
|
|
|
在宽字符串中查找宽字符的首次出现
(函数) |
|
|
查找字符的最后一次出现
(函数) |
|
|
查找分隔符集合中任意字符的首次出现位置
(函数) |
|
|
C 文档
for
strchr
|
|