strstr

王朝百科·作者佚名  2010-01-22
窄屏简体版  字體: |||超大  

原型:extern char *strstr(char *haystack, char *needle);

用法:#include <string.h> (经过多次试验,这个头文件也可以不包含,并不影响该函数的使用。既然MSDN上要求, 可以不妨带上)

功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。

说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。

举例:

// strstr.c

#include <syslib.h>

#include <string.h>

main()

{

char *s="Golden Global View";

char *l="lob";

char *p;

clrscr();

p=strstr(s,l);

if(p)

printf("%s",p);

else

printf("Not Found!");

getchar();

return 0;

}

语法:int strstr(str1,str2)

str1: 被查找目标string expression to search.

str2:要查找对象The string expression to find.

该函数返回str2第一次在str1中的位置,如果没有找到,返回NULL

The strstr() function returns the ordinal position within str1 of the first occurrence of str2. If str2 is not found in str1, strstr() returns 0.

例子:

功能:从字串” string1 onexxx string2 oneyyy”中寻找”yyy”

(假设xxx和yyy都是一个未知的字串)

char *s=” string1 onexxx string2 oneyyy”;

char *p;

p=strstr(s,”string2”);

if(!p) printf(“Not Found!”);

p=strstr(p,”one”);

if(!p) printf(“Not Found!”);

p+=strlen(“one”)

printf(“%s”,p);

说明:如果直接写语句p=strstr(p,”one”),则找到的是xxx,不符合要求

所以需采用二次查找法找到目标

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航