strstr
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <string.h> 中定义
|
||
const char *strstr( const char* str, const char* substr ); |
||
char strstr( char* str, const char* substr ); |
||
发现第一次出现的字节串
substr
中的字节串所指向的str
. 原文:
Finds the first occurrence of the byte string
substr
in the byte string pointed to by str
. 目录 |
[编辑] 参数
str | - | 检查null结尾的字节串的指针
原文: pointer to the null-terminated byte string to examine |
substr | - | 以NULL结尾的字节要搜索的字符串指针
原文: pointer to the null-terminated byte string to search for |
[编辑] 返回值
str
,或NULL中找到的子串的第一个字符的指针,如果没有这样的子被发现。如果substr
指向一个空字符串,str
返回.原文:
Pointer to the first character of the found substring in
str
, or NULL if no such substring is found. If substr
points to an empty string, str
is returned.[编辑] 示例
#include <string.h> #include <stdio.h> void find_str(char const* str, char const* substr) { char* pos = strstr(str, substr); if(pos) { printf("found the string '%s' in '%s' at position: %d\n", substr, str, pos - str); } else { printf("the string '%s' was not found in '%s'\n", substr, str); } } int main(int argc, char* argv[]) { char* 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
[编辑] 另请参阅
找到第一个出现的字符 原文: finds the first occurrence of a character (函数) | |
找到最后出现的字符 原文: finds the last occurrence of a character (函数) | |
C++ documentation for strstr
|