C:评估部分字符串

Vla*_*hev 4 c string substring

我找不到一个表达式来评估字符串的一部分.

我希望得到类似的东西:

if (string[4:8]=='abc') {...}
Run Code Online (Sandbox Code Playgroud)

我开始写这样的:

if (string[4]=='a' && string[5]=='b' && string[6]=='c') {...}
Run Code Online (Sandbox Code Playgroud)

但如果我需要评估字符串的大部分

if (string[10:40] == another_string) {...}
Run Code Online (Sandbox Code Playgroud)

然后它会写出太多的表达式.有没有现成的解决方案?

Dav*_*ley 6

你总是可以使用strncmp(),所以string[4:8] == "abc"(当然不是C语法)可能会变成strncmp(string + 4, "abc", 5) == 0.