比较两个字符串,而不考虑perl中的大小 [英] Compare two strings regardless of case size in perl
问题描述
有无论如何比较两个字符串,无论大小是什么?
例如
steveeqSTevE<-----这些会匹配
SHOEeqshoe
你会得到图片
是 - 使用 uc()
(大写函数;参见 http://perldoc.perl.org/functions/uc.html )
$ perl -e'print uc(steve)eq uc(STevE); print\ n;'
1
$ perl -e'print uc(SHOE)eq uc(shoe); print\ n;'
1
$ perl5.8 -e'print uc(SHOE)eq uc(shoe1); print\\\
;'
$
lc()
以及。
如果你希望实际的eq运算符不区分大小写,可能使用重载,但我不认为这是你要求 - 请澄清你的问题,如果是这样的情况。也不是一个好主意,如果你确实想要,IMHO - 太脆弱,导致主要可能很难跟踪和调试错误。
此外,这是一个overkill在你的具体情况下,你只是想要平等,但Perl正则表达式也有大小写无关的修饰符i
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
yes - use uc()
(upper-case function; see http://perldoc.perl.org/functions/uc.html )
$ perl -e 'print uc("steve") eq uc("STevE"); print "\n";'
1
$ perl -e 'print uc("SHOE") eq uc("shoe"); print "\n";'
1
$ perl5.8 -e 'print uc("SHOE") eq uc("shoe1"); print "\n";'
$
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"
这篇关于比较两个字符串,而不考虑perl中的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!