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.'
二次
另见
find | find characters in the string (public member function of std::basic_string) |
---|---|
wcsstr | finds the first occurrence of a wide string within another wide string (function) |
strchr | finds the first occurrence of a character (function) |
strrchr | finds the last occurrence of a character (function) |
c strstr文件
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。