In Str§
See primary documentation in context for method substr-eq
multi method substr-eq(Str:D: Str(Cool) $test-string, Int(Cool) $from, :i(:$ignorecase), :m(:$ignoremark) --> Bool) multi method substr-eq(Cool:D: Str(Cool) $test-string, Int(Cool) $from, :i(:$ignorecase), :m(:$ignoremark) --> Bool)
Returns True
if the $test-string
exactly matches the String
object, starting from the given initial index $from
. For example, beginning with the string "foobar"
, the substring "bar"
will match from index 3:
my $string = "foobar"; say $string.substr-eq("bar", 3); # OUTPUT: «True»
However, the substring "barz"
starting from index 3 won't match even though the first three letters of the substring do match:
my $string = "foobar"; say $string.substr-eq("barz", 3); # OUTPUT: «False»
Naturally, to match the entire string, one merely matches from index 0:
my $string = "foobar"; say $string.substr-eq("foobar", 0); # OUTPUT: «True»
Since Rakudo version 2020.02, if the optional named parameter :ignorecase
, or :i
, is specified, the comparison of the invocant and $test-string
ignores the distinction between uppercase, lowercase and titlecase letters.
say "foobar".substr-eq("Bar", 3); # OUTPUT: «False» say "foobar".substr-eq("Bar", 3, :ignorecase); # OUTPUT: «True»
Since Rakudo version 2020.02, if the optional named parameter :ignoremark
, or :m
, is specified, the comparison of the invocant and $test-string
only considers base characters, and ignores additional marks such as combining accents.
say "cliché".substr-eq("che", 3); # OUTPUT: «False» say "cliché".substr-eq("che", 3, :ignoremark); # OUTPUT: «True»
Since this method is inherited from the Cool
type, it also works on integers. Thus the integer 42
will match the value 342
starting from index 1:
my $integer = 342; say $integer.substr-eq(42, 1); # OUTPUT: «True»
As expected, one can match the entire value by starting at index 0:
my $integer = 342; say $integer.substr-eq(342, 0); # OUTPUT: «True»
Also using a different value or an incorrect starting index won't match:
my $integer = 342; say $integer.substr-eq(42, 3); # OUTPUT: «False» say $integer.substr-eq(7342, 0); # OUTPUT: «False»