This question already has an answer here:
这个问题在这里已有答案:
- How to compare ends of strings in C? 17 answers
如何比较C中字符串的结尾? 17个答案
How can i check whether a string ends with ".csv" in C
如何检查字符串是否以C中的“.csv”结尾
I've tried using strlen
without any success
我尝试过使用strlen而没有任何成功
3 个解决方案
#1
37
How about:
char *dot = strrchr(str, '.');
if (dot && !strcmp(dot, ".csv"))
/* ... */
#2
26
if(strlen(str) > 4 && !strcmp(str + strlen(str) - 4, ".csv"))
#3
4
The simplest (and most general) form of ThiefMaster's code would be:
ThiefMaster代码最简单(也是最常见)的形式是:
int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}
#1
37
How about:
char *dot = strrchr(str, '.');
if (dot && !strcmp(dot, ".csv"))
/* ... */
#2
26
if(strlen(str) > 4 && !strcmp(str + strlen(str) - 4, ".csv"))
#3
4
The simplest (and most general) form of ThiefMaster's code would be:
ThiefMaster代码最简单(也是最常见)的形式是:
int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}