如何检查字符串是否以Perl中的空格开头?
答
不需要正则表达式是:
substr($str, 0, 1) eq " "
答
你不需要substr
或这个正则表达式!
ord($str) == 32
而且,如果你在做这些比较的十亿,你应该注意到一个英俊的性能提升,以及:
use Benchmark qw(cmpthese);
my $str = " hello";
cmpthese(0, {
regex => sub { $str =~ /^/},
substr => sub { substr($str, 0, 1) eq ' ' },
ord => sub { ord($str) == 32 },
});
结果:
Rate regex substr ord
regex 6473675/s -- -43% -70%
substr 11300632/s 75% -- -48%
ord 21653474/s 234% 92% --
'$海峡=〜/^ \ s /' – paddy
你对[Perl文档](http://perldoc.perl.org/)的搜索对你有什么启示? – Borodin