不区分大小写的字符串比较C ++ [英] Case insensitive string comparison C++

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

问题描述

我知道有一些方法可以做case忽略比较,涉及迭代通过字符串或一个好的一个对SO需要另一个图书馆。我需要把它放在其他可能没有安装它的电脑。有没有办法使用标准库来做到这一点?现在我只是做...

I know there are ways to do case ignore comparison that involve iterating through strings or one good one on SO needs another library. I need to put this on other computers that might not have it installed. Is there a way to use the standard libraries to do this? Right now I am just doing...

if (foo == "Bar" || foo == "bar")
{
cout << "foo is bar" << endl;
}

else if (foo == "Stack Overflow" || foo == "stack Overflow" || foo == "Stack overflow" || foo == "etc.")
{
cout << "I am too lazy to do the whole thing..." << endl;
}

这可以大大提高我的代码的可读性和可用性。感谢您阅读这篇文章。

This could drastically improve the readability and usability of my code. Thanks for reading this far.

推荐答案

strncasecmp


strcasecmp()函数对字符串 s1 s2 执行逐字节比较,忽略字符。如果分别找到 s1 ,则返回小于,等于或大于零的整数,以小于,匹​​配或大于 s2

The strcasecmp() function performs a byte-by-byte comparison of the strings s1 and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

strncasecmp()函数是类似的,除了它比较不超过 n 字节 s1 s2 ...

The strncasecmp() function is similar, except that it compares no more than n bytes of s1 and s2...

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

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