strstr
#include<stdio.h> #include<string.h> int main( int argc, char *argv[] ) { int rc=0; char *str = "http://www.bbb.com"; char *p = strstr(str,"www"); 结果:www.bbb.com
char *p1 = strchr(str, '/'); // 结果://www.bbb.com printf("hehe,%s p:%s %u\n", str,p, __LINE__); return rc; }
结果:
$ gcc -o str.o str.c $ ./str.o $ http://www.bbb.com p:www.bbb.com 9
字符串后向匹配
int main( int argc, char *argv[] ) { int rc=0; char *visit = "http://www.bbb.com/eee"; char *p; char v_http[32],url[32]; if(!strncmp(visit,"http://",7)){ strcpy (v_http, visit+7); p = strchr(v_http, '/'); if(p){ strncpy (url, v_http, p - v_http); } printf("1 url %s p:%s v_http %s %lu\n", url,p, v_http,p - visit); }else{ strncpy (v_http, visit+4, p - visit); printf("2 visit %s p:%s str1 %s %lu\n", visit,p, v_http,p - visit); } char *list = "bbb.com"; rc = strncmp(list, url, strlen(list)); char *p1 = strstr(url,list); printf("3 visit %s list %s rc %d %s\n", visit,list,rc,p1); return rc; } # gcc -o str2.o str2.c # ./str2.o 1 url www.bbb.com p:/eee v_http www.bbb.com/eee 140731426069911 3 visit http://www.bbb.com/eee list bbb.com rc -21 bbb.com