如何在C [复制]中检查字符串是否以“.csv”结尾

时间:2021-02-18 07:21:57

This question already has an answer here:

这个问题在这里已有答案:

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