In Cool§

See primary documentation in context for method contains

method contains(Cool:D: |c)

Coerces the invocant to a Str, and calls Str.contains on it. Please refer to that version of the method for arguments and general syntax.

say 123.contains("2")# OUTPUT: «True␤»

Since Int is a subclass of Cool, 123 is coerced to a Str and then contains is called on it.

say (1,1* + * … * > 250).contains(233)# OUTPUT: «True␤»

Seqs are also subclasses of Cool, and they are stringified to a comma-separated form. In this case we are also using an Int, which is going to be stringified also; "233" is included in that sequence, so it returns True. Please note that this sequence is not lazy; the stringification of lazy sequences does not include each and every one of their components for obvious reasons.

In Str§

See primary documentation in context for method contains

multi method contains(Str:D: Cool:D $needle:i(:$ignorecase), :m(:$ignoremark--> Bool:D)
multi method contains(Str:D: Str:D $needle:i(:$ignorecase), :m(:$ignoremark--> Bool:D)
multi method contains(Str:D: Regex:D $needle --> Bool:D)
multi method contains(Str:D: Cool:D $needleInt(Cool:D$pos:i(:$ignorecase), :m(:$ignoremark--> Bool:D)
multi method contains(Str:D: Str:D $needleInt:D $pos:i(:$ignorecase), :m(:$ignoremark--> Bool:D)
multi method contains(Str:D: Regex:D $needleInt:D $pos --> Bool:D)
multi method contains(Str:D: Regex:D $needleCool:D $pos --> Bool:D)

Given a Str invocant (known as the haystack) and a first argument (known as the $needle), it searches for the $needle in the haystack from the beginning of the string and returns True if $needle is found. If the optional parameter $pos is provided, then contains will search the haystack starting from $pos characters into the string.

say "Hello, World".contains('Hello');      # OUTPUT: «True␤» 
say "Hello, World".contains('hello');      # OUTPUT: «False␤» 
say "Hello, World".contains('Hello'1);   # OUTPUT: «False␤» 
say "Hello, World".contains(',');          # OUTPUT: «True␤» 
say "Hello, World".contains(','3);       # OUTPUT: «True␤» 
say "Hello, World".contains(','10);      # OUTPUT: «False␤»

In the first case, contains searches for the 'Hello' string on the invocant right from the start of the invocant string and returns True. In the third case, the 'Hello' string is not found since we have started looking from the second position (index 1) in 'Hello, World'.

Since Rakudo version 2020.02, the $needle can also be a Regex in which case the contains method quickly returns whether the regex matches the string at least once. No Match objects are created, so this is relatively fast.

say 'Hello, World'.contains(/\w <?before ','>/);    # OUTPUT: «True␤» 
say 'Hello, World'.contains(/\w <?before ','>/5); # OUTPUT: «False␤»

Since Rakudo version 2020.02, if the optional named parameter :ignorecase, or :i, is specified, the search for $needle ignores the distinction between upper case, lower case and title case letters.

say "Hello, World".contains("world");              # OUTPUT: «False␤» 
say "Hello, World".contains("world":ignorecase); # OUTPUT: «True␤»

Since Rakudo version 2020.02, if the optional named parameter :ignoremark, or :m, is specified, the search for $needle only considers base characters, and ignores additional marks such as combining accents.

say "abc".contains("ä");               # OUTPUT: «False␤» 
say "abc".contains("ä":ignoremark);  # OUTPUT: «True␤»

Note that because of how a List or Array is coerced into a Str, the results may sometimes be surprising.

say <Hello, World>.contains('Hello');    # OUTPUT: «True␤» 
say <Hello, World>.contains('Hello'0); # OUTPUT: «True␤» 
say <Hello, World>.contains('Hello'1); # OUTPUT: «False␤»

See traps.