std::strchr
来自cppreference.com
<tbody>
</tbody>
| 在标头 <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 << "找到以 '" << target
<< "' 开始的 '" << result << "'\n";
// 增加结果,否则会在同一位置找到目标
++result;
}
}
输出:
找到以 'T' 开始的 'Try not. Do, or do not. There is no try.'
找到以 'T' 开始的 'There is no try.'
参阅
| 在数组中搜索字符的首次出现 (函数) | |
| 寻找给定子串的首次出现 ( std::basic_string<CharT,Traits,Allocator> 的公开成员函数)
| |
| 寻找宽字符串中宽字符的首次出现 (函数) | |
| 寻找字符的最后出现 (函数) | |
| 寻找任何来自分隔符集合的字符的首个位置 (函数) | |
strchr 的 C 文档
| |