strstr 匹配 c 中的第一次出现

发布于 2024-08-27 13:07:42 字数 283 浏览 8 评论 0原文

我想知道如果 str1 包含字符串:我如何匹配 str1 中的字符串 "Just"

"this is Just/1.1.249.4021 a test" 
// "Just" will always be the same

我正在尝试使用它来匹配它strstr 但到目前为止它不会匹配,因为 /...

关于如何匹配它有什么建议吗?谢谢

I was wondering how could I match the string "Just" in str1 if str1 contains the strings as:

"this is Just/1.1.249.4021 a test" 
// "Just" will always be the same

I'm trying to match it using strstr but so far it won't match because of the /...

Any suggestions on how to match it? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×纯※雪 2024-09-03 13:07:42

这对我有用——你呢?

#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);
}

This works for me - what about you?

#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);
}
最好是你 2024-09-03 13:07:42

您使用 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));

Something must be wrong with how you use strstr()
The following code works just fine...

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));
心房的律动 2024-09-03 13:07:42

如果字符串实际上是“Just/1.1.249.4021”,那么它将无法找到“just”,因为strstr区分大小写。如果您需要不区分大小写的版本,则必须编写自己的版本或 Google 了解现有实施。

If the string is actually "Just/1.1.249.4021" then it would fail to find "just", because strstr is case-sensitive. If you need a case-insensitive version you have to write your own or Google for an existing implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文