多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# C `strstr()`函数 > 原文: [https://beginnersbook.com/2017/11/c-strstr-function/](https://beginnersbook.com/2017/11/c-strstr-function/) **`strstr()`函数**在指定主字符串中搜索给定字符串,并返回指向给定字符串第一次出现的指针。 ## C `strstr()`函数声明 ```c char *strstr(const char *str, const char *searchString) ``` `str` - 要搜索的字符串。 `searchString` - 我们需要在其中搜索字符串`str`的字符串 ## `strstr()`的返回值 此函数返回指向给定字符串第一次出现的指针,这意味着如果我们打印此函数的返回值,它应显示主字符串的一部分,从给定字符串开始直到主字符串末尾。 ## 示例:C 中的`strstr()`函数 ```c #include <stdio.h> #include <string.h> int main () { const char str[20] = "Hello, how are you?"; const char searchString[10] = "you"; char *result; /* This function returns the pointer of the first occurrence * of the given string (i.e. searchString) */ result = strstr(str, searchString); printf("The substring starting from the given string: %s", result); return 0; } ``` 输出: ```c The substring starting from the given string: you? ``` 正如您所看到的那样,我们正在使用函数`strstr()`在字符串`"Hello, how are you?"`中搜索字符串`"you"`。由于函数将返回字符串`"you"`的第一次出现的指针,因此字符串`str`的从`"you"`开始的子字符串已被打印为输出。 #### 相关文章: 1. [C - `strspn()`示例](https://beginnersbook.com/2017/11/c-strspn-function/) 2. [C - `strrchr()`示例](https://beginnersbook.com/2017/11/c-strrchr-function/) 3. [C - `strcpy()`示例](https://beginnersbook.com/2017/11/c-strcpy-function/) 4. [C - `strcmp()`示例](https://beginnersbook.com/2017/11/c-strcmp-function/)