在 Perl 中比较两个字符串,无论大小写如何

发布于 2024-09-12 12:02:44 字数 142 浏览 1 评论 0原文

无论如何,无论大小写如何,都可以比较两个字符串吗? 例如

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2024-09-19 12:02:44

是 - 使用 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 "\n";'

$

显然您也可以使用 lc()

如果您希望实际的“eq”运算符不区分大小写,则可能可以使用重载,但我认为这不是您所要求的 - 如果是这种情况,请澄清您的问题。如果您确实想要的话,这也不是一个好主意,恕我直言 - 太脆弱并导致可能难以跟踪和调试的重大错误。

另外,在您只想要相等的特定情况下,这是一种矫枉过正,但 Perl 正则表达式也有与大小写无关的修饰符“i”

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"

涫野音 2024-09-19 12:02:44

有几种方法可以做到这一点:

A couple of ways to do this:

  • Use the lc or uc operator, which converts both strings to lower or upper case respectively:

    lc "steve" eq lc "STevE";
    
  • A simple regex will do just as well:

    'steve' =~ /^STevE$/i;
    
叹梦 2024-09-19 12:02:44

根据 perldoc (http://perldoc.perl.org/functions/fc.html),最好使用

fc("steve") eq fc("STevE")

在第一次使用fc之前不要忘记添加这一行:

use feature 'fc';

According to perldoc (http://perldoc.perl.org/functions/fc.html), it's better to use

fc("steve") eq fc("STevE")

Don't forget to add this line before first use of fc:

use feature 'fc';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文