Home C&C++函数库 c++ 语法 程序源码 Linux C库

strstr

函数   <cstring>

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );


查找子字符串

返回一个指向str1中第一次出现str2的指针,如果str2不是str1的一部分,则返回一个空指针。

匹配过程不包括结束空字符,它将在此停止。

☲  参数


str1
要扫描的C字符串。

str2
包含要匹配的字符序列的C字符串。

☉  返回值



一个指向在str2中指定的整个字符序列在str1中第一次出现的指针, 如果该序列在str1中不存在,则为空指针。

❆  可移植性



在C语言中,这个函数只声明为:

char * strstr ( const char *, const char * );

而不是c++中提供的两个重载版本。

☣  示例



/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  if (pch != NULL)
    strncpy (pch,"sample",6);
  puts (str);
  return 0;
}



这个例子在str中搜索"simple"子字符串,并将其替换为"sample"。

输出:
This is a sample string

🍄  另请参阅



strspn 获取字符串中字符集合的长度(function )
strchr 定位字符串中第一个出现的字符(function )
strpbrk 定位字符串中的字符(function )

联系我们 免责声明 关于CandCplus 网站地图