strstr
来自 cppreference.cn
定义于头文件 <string.h> |
||
char* strstr( const char* str, const char* substr ); |
(1) | |
/*QChar*/* strstr( /*QChar*/* str, const char* substr ); |
(2) | (自 C23 起) |
1) 查找在 null 结尾的字节字符串 str 中,首次出现的 null 结尾的字节字符串 substr。不比较终止 null 字符。
2) 类型通用函数,等同于 (1)。令
T
为不合格字符对象类型。- 如果
str
的类型是 const T*,则返回类型是 const char*。 - 否则,如果 `str` 的类型为 `T*`,则返回类型为 `char*`。
- 否则,行为未定义。
- 如果
如果 str 或 substr 不是指向 null 结尾的字节字符串的指针,则行为未定义。
目录 |
[编辑] 参数
str | - | 指向要检查的 null 结尾的字节字符串的指针 |
substr | - | 指向要搜索的 null 结尾的字节字符串的指针 |
[编辑] 返回值
指向 str 中找到的子字符串的第一个字符的指针,如果未找到此类子字符串,则为 null 指针。如果 substr 指向空字符串,则返回 str。
[编辑] 示例
运行此代码
#include <stdio.h> #include <string.h> void find_str(char const* str, char const* substr) { char const* pos = strstr(str, substr); if (pos) printf( "Found the string [%s] in [%s] at position %td\n", substr, str, pos - str ); else printf( "The string [%s] was not found in [%s]\n", substr, str ); } int main(void) { char const* str = "one two three"; find_str(str, "two"); find_str(str, ""); find_str(str, "nine"); find_str(str, "n"); return 0; }
输出
Found the string [two] in [one two three] at position 4 Found the string [] in [one two three] at position 0 The string [nine] was not found in [one two three] Found the string [n] in [one two three] at position 1
[编辑] 参考
- C23 标准 (ISO/IEC 9899:2024)
- 7.24.5.7 strstr 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.24.5.7 strstr 函数 (p: 269)
- C11 标准 (ISO/IEC 9899:2011)
- 7.24.5.7 strstr 函数 (p: 369)
- C99 标准 (ISO/IEC 9899:1999)
- 7.21.5.7 strstr 函数 (p: 332)
- C89/C90 标准 (ISO/IEC 9899:1990)
- 4.11.5.7 strstr 函数
[编辑] 另请参见
查找字符的第一次出现 (函数) | |
查找字符的最后一次出现 (函数) | |
C++ 文档 for strstr
|