我想知道如果包含字符串"Just"
,我怎么能匹配字符串:str1
str1
"this is Just/1.1.249.4021 a test"
// "Just" will always be the same
我正在尝试使用它来匹配它,strstr
但到目前为止它不会匹配,因为/...
关于如何匹配它的任何建议?谢谢
这对我有用——你呢?
#include <string.h>
#include <stdio.h>
int main(void)
{
char haystack[] = "this is just\2323 a test";
char needle[] = "just";
char *loc = strstr(haystack, needle);
if (loc == 0)
printf("Did not find <<%s>> in <<%s>>\n", needle, haystack);
else
printf("Found <<%s>> in <<%s> at <<%s>>\n", needle, haystack, loc);
return(0);
}
您使用 strstr() 的方式一定有问题以下代码可以正常工作...
const char *s = "this is just\2323 a test";
char *p = strstr(s, "just");
if(p)
printf("Found 'just' at index %d\n", (int)(p - s));
如果字符串实际上是“Just/1.1.249.4021”,那么它将无法找到“just”,因为strstr
它区分大小写。如果您需要不区分大小写的版本,您必须为现有的实现编写自己的版本或Google 。