In Cool§
See primary documentation in context for method contains
method contains(Cool: |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»
Seq
s 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: Cool , :i(:), :m(:) --> Bool)multi method contains(Str: Str , :i(:), :m(:) --> Bool)multi method contains(Str: Regex --> Bool)multi method contains(Str: Cool , Int(Cool) , :i(:), :m(:) --> Bool)multi method contains(Str: Str , Int , :i(:), :m(:) --> Bool)multi method contains(Str: Regex , Int --> Bool)multi method contains(Str: Regex , Cool --> Bool)
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 /); # OUTPUT: «True»say 'Hello, World'.contains(/\w /, 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 uppercase, lowercase, and titlecase 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.