在 Perl 中比较两个字符串,无论大小写如何
无论如何,无论大小写如何,都可以比较两个字符串吗? 例如
"steve" eq "STevE" <----- these would match
"SHOE" eq "shoe"
你得到了图片
is there anyway to compare two strings regardless of case size?
For Example
"steve" eq "STevE" <----- these would match
"SHOE" eq "shoe"
You get the picture
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是 - 使用
uc()
(大写函数;请参阅 http:// /perldoc.perl.org/functions/uc.html )显然您也可以使用
lc()
。如果您希望实际的“eq”运算符不区分大小写,则可能可以使用重载,但我认为这不是您所要求的 - 如果是这种情况,请澄清您的问题。如果您确实想要的话,这也不是一个好主意,恕我直言 - 太脆弱并导致可能难以跟踪和调试的重大错误。
另外,在您只想要相等的特定情况下,这是一种矫枉过正,但 Perl 正则表达式也有与大小写无关的修饰符“i”
yes - use
uc()
(upper-case function; see http://perldoc.perl.org/functions/uc.html )You can obviously use
lc()
as well.If you want the actual "eq" operator to be case insensitive, it might be possible using overloads but I don't think that's what you are asking for - please clarify your question if that's the case. Nor is it a great idea if you do want that, IMHO - too fragile and leads to major possible hard to trace and debug bugs.
Also, it's an overkill in your specific case where you just want equality, but Perl regular expressions also have case-independent modifyer "i"
有几种方法可以做到这一点:
使用
lc
< /a> 或uc
运算符,分别将两个字符串转换为小写或大写:简单的正则表达式也可以:
A couple of ways to do this:
Use the
lc
oruc
operator, which converts both strings to lower or upper case respectively:A simple regex will do just as well:
根据 perldoc (http://perldoc.perl.org/functions/fc.html),最好使用
在第一次使用
fc
之前不要忘记添加这一行:According to perldoc (http://perldoc.perl.org/functions/fc.html), it's better to use
Don't forget to add this line before first use of
fc
: