strstr
From cppreference.net
|
定义于头文件
<string.h>
|
||
|
char
*
strstr
(
const
char
*
str,
const
char
*
substr
)
;
|
(1) | |
|
/*QChar*/
*
strstr
(
/*QChar*/
*
str,
const
char
*
substr
)
;
|
(2) | (C23 起) |
1)
在由
str
指向的以空字符结尾的字节字符串中,查找由
substr
指向的以空字符结尾的字节字符串的首次出现。终止空字符不参与比较。
2)
与
(1)
等价的泛型函数。令
T
为无限定字符对象类型。
-
-
若
str的类型为 const T * ,则返回类型为 const char * 。 -
否则,若
str的类型为 T * ,则返回类型为 char * 。 - 否则行为未定义。
-
若
如果 str 或 substr 不是指向以空字符结尾的字节字符串的指针,则行为未定义。
目录 |
参数
| str | - | 指向要检查的空终止字节字符串的指针 |
| substr | - | 指向要搜索的空终止字节字符串的指针 |
返回值
指向在 str 中找到的子字符串首字符的指针,如果未找到该子字符串则返回空指针。若 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 函数 (页: 269)
- C11 标准 (ISO/IEC 9899:2011):
-
- 7.24.5.7 strstr 函数 (p: 369)
- C99标准(ISO/IEC 9899:1999):
-
- 7.21.5.7 strstr函数(页码:332)
- C89/C90 标准 (ISO/IEC 9899:1990):
-
- 4.11.5.7 strstr 函数
参见
|
查找字符的首次出现
(函数) |
|
|
查找字符的最后一次出现
(函数) |
|
|
C++ 文档
关于
strstr
|
|