See Original text in context
multi method substr-eq(Str: Str(Cool) , Int(Cool) , :i(:), :m(:) --> Bool)multi method substr-eq(Cool: Str(Cool) , Int(Cool) , :i(:), :m(:) --> 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 = "foobar";say .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 = "foobar";say .substr-eq("barz", 3); # OUTPUT: «False»
Naturally, to match the entire string, one merely matches from index 0:
my = "foobar";say .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 upper case, lower case and title case 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 = 342;say .substr-eq(42, 1); # OUTPUT: «True»
As expected, one can match the entire value by starting at index 0:
my = 342;say .substr-eq(342, 0); # OUTPUT: «True»
Also using a different value or an incorrect starting index won't match:
my = 342;say .substr-eq(42, 3); # OUTPUT: «False»say .substr-eq(7342, 0); # OUTPUT: «False»