std::strstr
来自 cppreference.cn
在头文件 <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 documentation for strstr
|