std::strstr
来自cppreference.com
| 在标头 <cstring> 定义
|
||
| |
||
| |
||
在 haystack 所指的字节字符串中寻找字节字符串 needle 的首次出现。不比较空终止字符。
参数
| haystack | - | 指向要检验的空终止字节字符串的指针 |
| needle | - | 指向要搜索的空终止字节字符串的指针 |
返回值
指向 haystack 中寻获子串的首个字符的指针,或若找不到该字符则为空指针。若 needle 指向空字符串,则返回 haystack。
示例
运行此代码
#include <cstring>
#include <iomanip>
#include <iostream>
int main()
{
const char *str = "Try not. Do, or do not. There is no try.";
const char *target = "not";
for (const char* result = str; (result = std::strstr(result, target)); ++result)
std::cout << "找到 " << std::quoted(target)
<< " 位于 (" << result - str << '): ';
<< std::quoted(result) << '\n';
}
输出:
找到 "not" 位于 (4): "not. Do, or do not. There is no try."
找到 "not" 位于 (19): "not. There is no try."
