我正在写一些Perl脚本,我需要做很多字符串匹配.例如:
my $str1 = "this is a test string";
my $str2 = "test";
Run Code Online (Sandbox Code Playgroud)
要查看$ str1是否包含$ str2 - 我发现有两种方法:
方法1:使用索引功能:
if ( index($str1, $str2) != -1 ) { .... }
Run Code Online (Sandbox Code Playgroud)
方法2:使用正则表达式:
if( $str1 =~ /$str2/ ) { .... }
Run Code Online (Sandbox Code Playgroud)
哪个更好?什么时候我们应该使用其中的每一个?