在线文档教程
C++
字符串 | Strings

std::strchr

STD::strchr

Defined in header
const char* strchr( const char* str, int ch
char* strchr( char* str, int ch

查找字符的第一次出现。static_cast<char>(ch)所指向的字节字符串中str...

终止空字符被认为是字符串的一部分。

参数

str-pointer to the null-terminated byte string to be analyzed
ch-character to search for

返回值

中找到的字符的指针。str,如果没有找到这样的字符,则为空指针。

二次

#include <iostream> #include <cstring> 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)) != NULL) { 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.'

二次

另见

findfind characters in the string (public member function of std::basic_string)
wcschrfinds the first occurrence of a wide character in a wide string (function)
strrchrfinds the last occurrence of a character (function)
strpbrkfinds the first location of any character from a set of separators (function)

C.strchr文件

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/string/字节/strchr