查找字符串s2是否存在于s1中



是否有C函数检查字符串s2是否存在于s1中?

s1: "CN1 CN2 CN3"
s2: "CN2"  or  "CG2"

s1是固定的,我想检查s1中是否存在s2的变体

可以使用strstr:

#include <string.h>
if (strstr(s1, s2) != NULL)
{
    // s2 exists in s1
}

您可以使用strstr。参见strstr文档

function strstr

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

在str指向的字节串中查找第一个出现的字节串substr。

一个示例用法如下:

 const char *s1 = "CN1 CN2 CN3";
 if (strstr(s1, "CN2") == NULL) //^^!=NULL means exist
 {
     //does not exist
 }

正如其他人提到的,您应该使用strstr()。为strstr()添加一个链接到GNU C文档,因为您提到您使用的是C而不是c++,但是,此函数的c++文档也适用于C。

GNU C字符串搜索函数

相关内容

最新更新