当我运行以下代码时,它不会进入“在此处执行某些操作”部分:
my $a ='µ╫P[┐╬♣3▀═<+·1╪מ└╖"ª';
my $b ='µ╫P[┐╬♣3▀═<+·1╪מ└╖"ª';
if ($a ne $b) {
# do something here
}
还有另一种方法可以将 Unicode 字符串与 perl 进行比较吗?
如果您有两个 Unicode 字符串(即 Unicode 代码点字符串),那么您肯定已将文件保存为 UTF-8 并且实际上有
use utf8; # Tell Perl source code is UTF-8.
my $a = 'µ╫P[┐╬♣3▀═<+·1╪מ└╖"ª';
my $b = 'µ╫P[┐╬♣3▀═<+·1╪מ└╖"ª';
if ($a eq $b) {
print("They're equal.\n");
} else {
print("They're not equal.\n");
}
这工作得很好。eq
and ne
将逐个代码点比较字符串的代码点。
某些字素(例如“é”)可以通过多种不同的方式构建,因此您可能必须正常化 http://search.cpan.org/perldoc?Unicode::Normalize首先是他们的代表。
use utf8; # Tell Perl source code is UTF-8.
use charnames qw( :full ); # For \N{}
use Unicode::Normalize qw( NFC );
my $a = NFC("\N{LATIN SMALL LETTER E WITH ACUTE}");
my $b = NFC("e\N{COMBINING ACUTE ACCENT}");
if ($a eq $b) {
print("They're equal.\n");
} else {
print("They're not equal.\n");
}
最后,Unicode 认为某些字符几乎等效,并且可以使用不同形式的规范化将它们视为相等。
use utf8; # Tell Perl source code is UTF-8.
use charnames qw( :full ); # For \N{}
use Unicode::Normalize qw( NFKC );
my $a = NFKC("2");
my $b = NFKC("\N{SUPERSCRIPT TWO}");
if ($a eq $b) {
print("They're equal.\n");
} else {
print("They're not equal.\n");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)