C语言 如何将一个字符串的最后 n 个字符与 C 中的另一个字符串进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5297248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Compare Last n Characters of A String to Another String in C
提问by sjor
Imagine that I have two strings, one of them is a url like "/sdcard/test.avi" and the other one is"/sdcard/test.mkv". I want to write an if statement that looks whether the last four characters of string is ".avi" or not in C. How can I do this? Using strcmp or what and how?
想象一下,我有两个字符串,其中一个是像“/sdcard/test.avi”这样的网址,另一个是“/sdcard/test.mkv”。我想写一个 if 语句,在 C 中查看字符串的最后四个字符是否为“.avi”。我该怎么做?使用 strcmp 或什么以及如何使用?
回答by Oliver Charlesworth
If you have a pointer-to-char array, str, then this:
如果你有一个指向字符数组的指针str,那么这个:
int len = strlen(str);
const char *last_four = &str[len-4];
will give you a pointer to the last four characters of the string. You can then use strcmp(). Note that you'll need to cope with the case where (len < 4), in which case the above won't be valid.
会给你一个指向字符串最后四个字符的指针。然后您可以使用strcmp(). 请注意,您需要处理 where 的情况(len < 4),在这种情况下,上述内容无效。
回答by Vlad
In pure C you can only resort to manual compare:
在纯 C 中,您只能求助于手动比较:
int endswith(const char* withwhat, const char* what)
{
int l1 = strlen(withwhat);
int l2 = strlen(what);
if (l1 > l2)
return 0;
return strcmp(withwhat, what + (l2 - l1)) == 0;
}
回答by Assaf Levy
Just perform if ( strcmp(str1+strlen(str1)-4, str2+strlen(str2)-4) == 0 ) {}.
只是执行if ( strcmp(str1+strlen(str1)-4, str2+strlen(str2)-4) == 0 ) {}。
Make sure both strings are at least 4 characters long.
确保两个字符串的长度至少为 4 个字符。
回答by John Byrne
How about this...
这个怎么样...
if (!strcmp(strrchr(str, 'int EndsWithTail(char *url, char* tail)
{
if (strlen(tail) > strlen(url))
return 0;
int len = strlen(url);
if (strcmp(&url[len-strlen(tail)],tail) == 0)
return 1;
return 0;
}
') - 4, ".avi")){
//The String ends with ".avi"
}
char *strrchr(const char *str, int c)- Returns a pointer to the last matching char found in the string, including the NULL char if you so specify. In this case, I use it to get a pointer to the end of the string and then I move the pointer 4 steps back, thus giving a Pointer to the last 4 chars of the string.
char *strrchr(const char *str, int c)- 返回指向在字符串中找到的最后一个匹配字符的指针,如果您指定,则包括 NULL 字符。在这种情况下,我使用它来获取指向字符串末尾的指针,然后将指针向后移动 4 步,从而提供指向字符串最后 4 个字符的指针。
I then compare the last 4 chars, to ".avi" and if they match, strcmp returns a 0 or logic FALSE, which I invert in my 'if' condition.
然后我将最后 4 个字符与“.avi”进行比较,如果它们匹配,则 strcmp 返回 0 或逻辑 FALSE,我在“if”条件中将其反转。
回答by Shamim Hafiz
Here is a generic function to test:
这是一个要测试的通用函数:
#include <dirent.h>
#include <string.h>
int main(void)
{
DIR *dir;
struct dirent *ent;
char files[100][500];
int i = 0;
memset(files, 0, 100*500);
dir = opendir ("/sdcard/");
if (dir != NULL)
{
/* Print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
strcpy(files[i], ent->d_name);
if(strstr(files[i], ".avi") != NULL)
{
printf("\n files[%d] : %s is valid app file\n", i, files[i]);
i++;
}
}
closedir (dir);
}
return 0;
}

