Namespaces
Variants

std:: strstr

From cppreference.net
定义于头文件 <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 << "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."

参见

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