method contains
1 | class Str |
1.1 | (Str) method contains |
2 | class Cool |
2.1 | (Cool) method contains |
Documentation for method contains
assembled from the following types:
class Str
From Str
(Str) method contains
Defined as:
multi method contains(Str: Cool --> Bool)multi method contains(Str: Str --> Bool)multi method contains(Str: Cool , Int(Cool) --> Bool)multi method contains(Str: Str , Int --> Bool)
Coerces the invocant (represented in the signature by Str:D:
, that would be the haystack) and first argument (which we are calling $needle
) to Str
(if it's not already, that is, in the first and third multi
forms), and searches for $needle
in the invocant (or haystack) starting from $pos
characters into the string, if that is included as an argument.
Returns True
if $needle
is found. $pos
is an optional parameter, and if it's not present, contains
will search from the beginning of the string (using the first two forms of the multi
).
say <Hello, World>.contains('Hello', 0); # OUTPUT: «True»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 example, coercion is used to convert a List
to a Str. In the 4th case, the 'Hello'
string is not found since we have started looking at the second position in it (index 1). Note that because of how a List or Array is coerced into a Str, the results may sometimes be surprising. See traps.
class Cool
From Cool
(Cool) method contains
Defined as:
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»
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.