strstr() 函数在 string.h 头文件中定义。它有助于返回指向给定完整字符串中匹配子字符串第一次出现的指针,该字符串由“string”指向。
char *strstr(const char *string, const char *match) ; #where string and match should be strings
strstr() 函数接受两个参数。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 字符串 | 将搜索子字符串的完整字符串 | 必需 |
| 匹配 | 要在完整字符串中搜索的子字符串 | 必需 |
该函数返回指向完整字符串中与匹配项中指定的整个字符序列第一次出现的指针。
| 输入 | 返回值 |
|---|---|
| 如果参数 | 指向匹配字符串第一次出现的指针 |
#include <stdio.h>
#include <string.h>
int main()
{
char string[100]="this is javatpoint with c and java";
char *s;
s=strstr(string,"java");
printf("\nThe substring is: %s",s);
return 0;
}
输出
javatpoint with c and java
#include <stdio.h>
#include <string.h>
int main (){
const char fulstrng[20] = "TutorialsPoint";
const char substrng[10] = "Point";
char *out;
out = strstr(fulstrng, substrng);
printf("The substring is: %s\n", out);
return(0);
}
输出
The substring is: Point