substr-eq
ErrorsCollection

substr-eq

Synthesised documentation from type/Str

From type/Str

See Original text in context

multi method substr-eq(Str:D:  Str(Cool$test-stringInt(Cool$from:i(:$ignorecase), :m(:$ignoremark--> Bool)
multi method substr-eq(Cool:D: Str(Cool$test-stringInt(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 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 $integer = 342;
say $integer.substr-eq(421);      # OUTPUT: «True␤»

As expected, one can match the entire value by starting at index 0:

my $integer = 342;
say $integer.substr-eq(3420);     # OUTPUT: «True␤»

Also using a different value or an incorrect starting index won't match:

my $integer = 342;
say $integer.substr-eq(423);      # OUTPUT: «False␤» 
say $integer.substr-eq(73420);    # OUTPUT: «False␤»