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

std::strstr

STD:STSTR

Defined in header
const char* strstr( const char* str, const char* target
char* strstr( char* str, const char* target

查找字节字符串的第一个匹配项。target所指向的字节字符串中str不对终止空字符进行比较。

参数

str-pointer to the null-terminated byte string to examine
target-pointer to the null-terminated byte string to search for

返回值

中找到的子字符串的第一个字符的指针。str,或NULL如果找不到这样的角色。如果target指向空字符串,str会被归还。

二次

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

二次

产出:

二次

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

二次

另见

findfind characters in the string (public member function of std::basic_string)
wcsstrfinds the first occurrence of a wide string within another wide string (function)
strchrfinds the first occurrence of a character (function)
strrchrfinds the last occurrence of a character (function)

c strstr文件

© cppreference.com

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

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