In Str§
See primary documentation in context for method index
multi method index(Str: Cool , :i(:), :m(:) --> Int)multi method index(Str: Str , :i(:), :m(:) --> Int)multi method index(Str: Cool , Cool , :i(:), :m(:) --> Int)multi method index(Str: Str , Int , :i(:), :m(:) --> Int)multi method index(Str: --> Int)multi method index(Str: , :m(:)! --> Int)multi method index(Str: , :i(:)!, :m(:) --> Int)
Searches for $needle
in the string starting from $pos
(if present). It returns the offset into the string where $needle
was found, and Nil
if it was not found.
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. In addition, 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.
Since Rakudo version 2020.05, index
accepts a list of needles to search the string with, and return the lowest index found or Nil
.
Examples:
say "Camelia is a butterfly".index("a"); # OUTPUT: «1»say "Camelia is a butterfly".index("a", 2); # OUTPUT: «6»say "Camelia is a butterfly".index("er"); # OUTPUT: «17»say "Camelia is a butterfly".index("Camel"); # OUTPUT: «0»say "Camelia is a butterfly".index("Onion"); # OUTPUT: «Nil»say "Camelia is a butterfly".index(<a e i u>); # OUTPUT: «1»say "Camelia is a butterfly".index(<a c>, :i); # OUTPUT: «0»say "Camelia is a butterfly".index(('w', 'x')); # OUTPUT: «Nil»say "Hello, World".index("world"); # OUTPUT: «Nil»say "Hello, World".index("world", :ignorecase); # OUTPUT: «7»say "abc".index("ä"); # OUTPUT: «Nil»say "abc".index("ä", :ignoremark); # OUTPUT: «0»say "abc".index("x").defined ?? 'OK' !! 'NOT'; # OUTPUT: «NOT»
Other forms of index
, including subs, are inherited from Cool
.
In Cool§
See primary documentation in context for routine index
multi index(Cool , Cool , :i(:), :m(:) --> Int)multi index(Cool , Cool , Cool , :i(:), :m(:) --> Int)multi method index(Cool: Cool --> Int)multi method index(Cool: Cool , :m(:)! --> Int)multi method index(Cool: Cool , :i(:)!, :m(:) --> Int)multi method index(Cool: Cool , Cool --> Int)multi method index(Cool: Cool , Cool , :m(:)! --> Int)multi method index(Cool: Cool , Cool , :i(:)!, :m(:) --> Int)
Coerces the first two arguments (in method form, also counting the invocant) to a Str
, and searches for $needle
in the string $s
starting from $pos
. It returns the offset into the string where $needle
was found, and Nil
if it was not found.
See the documentation in type Str for examples.