Anonymous user
String matching: Difference between revisions
→{{header|Perl}}: present the answer in a more reader-friendly way
(Updated D entry) |
(→{{header|Perl}}: present the answer in a more reader-friendly way) |
||
Line 1,551:
=={{header|Perl}}==
Using regexes:
<lang perl>$str1 =~ /\Q$str2\E/ # true if $str1 contains $str2
$str1 =~ /\Q$str2\E$/ # true if $str1 ends with $str2</lang>
Using <code>index</code>:
<lang perl>index($str1, $str2) != -1 # true if $str1 contains $str2
index($str1, $str2) == 0 # true if $str1 starts with $str2
rindex($str1, $str2) == length($str1) - length($str2) # true if $str1 ends with $str2</lang>
Using <code>substr</code>:
<lang perl>substr($str1, 0, length($str2)) eq $str2 # true if $str1 starts with $str2
substr($str1, - length($str2)) eq $str2 # true if $str1 ends with $str2</lang>
=={{header|Perl 6}}==
|