C 中不区分大小写的字符串比较 [英] Case Insensitive String Comparison in C

查看:53
本文介绍了C 中不区分大小写的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个邮政编码 char* 我想比较,忽略大小写.有没有功能可以做到这一点?

I have two postcodes char* that I want to compare, ignoring case. Is there a function to do this?

或者我是否必须遍历每个使用的 tolower 函数然后进行比较?

Or do I have to loop through each use the tolower function and then do the comparison?

知道这个函数如何对字符串中的数字做出反应

Any idea how this function will react with numbers in the string

谢谢

推荐答案

C 标准中没有执行此操作的函数.符合 POSIX 的 Unix 系统需要有 strcasecmp 在头文件 strings.h 中;Microsoft 系统具有 stricmp.为了便于携带,请自行编写:

There is no function that does this in the C standard. Unix systems that comply with POSIX are required to have strcasecmp in the header strings.h; Microsoft systems have stricmp. To be on the portable side, write your own:

int strcicmp(char const *a, char const *b)
{
    for (;; a++, b++) {
        int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
        if (d != 0 || !*a)
            return d;
    }
}

但请注意,这些解决方案都不适用于 UTF-8 字符串,只能使用 ASCII 字符串.

But note that none of these solutions will work with UTF-8 strings, only ASCII ones.

这篇关于C 中不区分大小写的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆