In Str§
See primary documentation in context for method index.
multi method index(Str:D: Cool:D $needle, :i(:$ignorecase), :m(:$ignoremark) --> Int:D) multi method index(Str:D: Str:D $needle, :i(:$ignorecase), :m(:$ignoremark) --> Int:D) multi method index(Str:D: Cool:D $needle, Cool:D $pos, :i(:$ignorecase), :m(:$ignoremark) --> Int:D) multi method index(Str:D: Str:D $needle, Int:D $pos, :i(:$ignorecase), :m(:$ignoremark) --> Int:D) multi method index(Str:D: @needles --> Int:D) multi method index(Str:D: @needles, :m(:$ignoremark)! --> Int:D) multi method index(Str:D: @needles, :i(:$ignorecase)!, :m(:$ignoremark) --> Int:D)
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:D $s, Cool:D $needle, :i(:$ignorecase), :m(:$ignoremark) --> Int:D) multi index(Cool:D $s, Cool:D $needle, Cool:D $pos, :i(:$ignorecase), :m(:$ignoremark) --> Int:D) multi method index(Cool:D: Cool:D $needle --> Int:D) multi method index(Cool:D: Cool:D $needle, :m(:$ignoremark)! --> Int:D) multi method index(Cool:D: Cool:D $needle, :i(:$ignorecase)!, :m(:$ignoremark) --> Int:D) multi method index(Cool:D: Cool:D $needle, Cool:D $pos --> Int:D) multi method index(Cool:D: Cool:D $needle, Cool:D $pos, :m(:$ignoremark)! --> Int:D) multi method index(Cool:D: Cool:D $needle, Cool:D $pos, :i(:$ignorecase)!, :m(:$ignoremark) --> Int:D)
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.