命名空间
变体
操作

std::strstr

来自 cppreference.cn
< cpp‎ | string‎ | byte
定义于头文件 <cstring>
const char* strstr( const char* haystack, const char* needle );
      char* strstr(       char* haystack, const char* needle );

haystack 所指向的字节字符串中,查找字节字符串 needle 首次出现的位置。 不比较终止空字符。

目录

[edit] 参数

haystack - 指向要检查的空终止字节字符串的指针
needle - 指向要搜索的空终止字节字符串的指针

[edit] 返回值

指向 haystack 中找到的子字符串的第一个字符的指针;如果未找到此类字符,则为空指针。 如果 needle 指向空字符串,则返回 haystack

[edit] 示例

#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 << "Found " << std::quoted(target)
                  << " starting at (" << result - str << "): "
                  << std::quoted(result) << '\n';
}

输出

Found "not" starting at (4): "not. Do, or do not. There is no try."
Found "not" starting at (19): "not. There is no try."

[edit] 参见

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