如何比较字符串忽略大小写 [英] How to compare strings ignoring the case

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

问题描述

我希望 appleApple 的比较为 true.目前

I want apple and Apple comparison to be true. Currently

"Apple" == "Apple"  # returns TRUE
"Apple" == "APPLE"  # returns FALSE

推荐答案

您正在寻找 casecmp.如果两个字符串相等,则返回 0,不区分大小写.

You're looking for casecmp. It returns 0 if two strings are equal, case-insensitively.

str1.casecmp(str2) == 0

"Apple".casecmp("APPLE") == 0
#=> true

或者,您可以将两个字符串都转换为小写(str.downcase) 并比较是否相等.

Alternatively, you can convert both strings to lower case (str.downcase) and compare for equality.

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

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