String matching: Difference between revisions

Line 1,918:
 
# Index-based:
sub I_contains ( Str $_, Str $s2 ) { .index( $s2)\ .defined },
sub I_starts_with ( Str $_, Str $s2 ) { my $m = .index( $s2); $m.defined and $m == 0 },
sub I_ends_with ( Str $_, Str $s2 ) { my $m = .rindex($s2); $m.defined and $m == .chars - $s2.chars },
Line 1,924:
# Substr-based:
sub S_starts_with ( Str $_, Str $s2 ) { .substr(0, $s2.chars) eq $s2 },
sub S_ends_with ( Str $_, Str $s2 ) { .substr( *-$s2.chars) eq $s2 },
 
# Optional tasks:
Line 1,939:
say "'$str1' vs:".fmt('%15s '), @str2s.fmt('%-15s');
for [1, 4, 6], [2, 5, 7], [0, 3], [8, 9] {
say();
for @subs[.list] -> $sub {
say "{$sub.name}:".fmt('%15s '),
Anonymous user