std::strstr

来自cppreference.com
<tbody> </tbody>
在标头 <cstring> 定义
const char* strstr( const char* haystack, const char* needle );
  char* strstr( char* haystack, const char* needle );

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."

参阅

寻找给定子串的首次出现
(std::basic_string<CharT,Traits,Allocator> 的公开成员函数) [编辑]
在另一宽字符串中寻找宽字符串的首次出现
(函数) [编辑]
寻找字符的首次出现
(函数) [编辑]
寻找字符的最后出现
(函数) [编辑]
strstr 的 C 文档