我想确保我的字符串以".foo"结尾.我正在使用C,这是一种我并不完全熟悉的语言.我发现这样做的最好方法是在下面.任何C大师都想确保我优雅而明智地做到这一点?
int EndsWithFoo(char *str)
{
if(strlen(str) >= strlen(".foo"))
{
if(!strcmp(str + strlen(str) - strlen(".foo"), ".foo"))
{
return 1;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
pli*_*nth 47
不要每个字符串多次调用strlen.
int EndsWith(const char *str, const char *suffix)
{
if (!str || !suffix)
return 0;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return 0;
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
}
int EndsWithFoo(const char *str) { return EndsWith(str, ".foo"); }
Run Code Online (Sandbox Code Playgroud)
编辑:添加NULL检查迂腐.对于超级迂腐,如果str和后缀都是NULL,它是否应该返回非零的争论.
小智 11
int EndsWithFoo( char *string )
{
string = strrchr(string, '.');
if( string != NULL )
return( strcmp(string, ".foo") );
return( -1 );
}
Run Code Online (Sandbox Code Playgroud)
如果以".foo"结尾,则返回0.
我现在无法访问编译器,所以有人可以告诉我这是否有效吗?
#include <stdio.h>
#include <string.h>
int EndsWithFoo(const char* s);
int
main(void)
{
printf("%d\n", EndsWithFoo("whatever.foo"));
return 0;
}
int EndsWithFoo(const char* s)
{
int ret = 0;
if (s != NULL)
{
size_t size = strlen(s);
if (size >= 4 &&
s[size-4] == '.' &&
s[size-3] == 'f' &&
s[size-2] == 'o' &&
s[size-1] == 'o')
{
ret = 1;
}
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
无论如何,请确保将参数限定为const,它告诉所有人(包括编译器)您不打算修改字符串.
小智 5
这是使用 memcmp() 返回与 Python str.endswith() 相同的值的通用解决方案。不检查 str / 后缀是否为 NULL 是有意的,其他 libc str 函数也不检查 NULL:
int ends_with(const char *str, const char *suffix) {
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
return (str_len >= suffix_len) &&
(!memcmp(str + str_len - suffix_len, suffix, suffix_len));
}
Run Code Online (Sandbox Code Playgroud)
测试C:
printf("%i\n", ends_with("", ""));
printf("%i\n", ends_with("", "foo"));
printf("%i\n", ends_with("foo", ""));
printf("%i\n", ends_with("foo", "foo"));
printf("%i\n", ends_with("foo", "foobar"));
printf("%i\n", ends_with("foo", "barfoo"));
printf("%i\n", ends_with("foobar", "foo"));
printf("%i\n", ends_with("barfoo", "foo"));
printf("%i\n", ends_with("foobarfoo", "foo"));
Run Code Online (Sandbox Code Playgroud)
结果C:
1
0
1
1
0
0
0
1
1
Run Code Online (Sandbox Code Playgroud)
测试Python:
print("".endswith(""))
print("".endswith("foo"))
print("foo".endswith(""))
print("foo".endswith("foo"))
print("foo".endswith("foobar"))
print("foo".endswith("barfoo"))
print("foobar".endswith("foo"))
print("barfoo".endswith("foo"))
print("foobarfoo".endswith("foo"))
Run Code Online (Sandbox Code Playgroud)
结果Python:
True
False
True
True
False
False
False
True
True
Run Code Online (Sandbox Code Playgroud)