Course of Raku / Advanced / More about built-in types / Strings

Searching in strings

Several methods answer questions about what a string contains.

The contains method tells whether one string appears anywhere inside another:

say 'Raku'.contains('ak'); # True
say 'Raku'.contains('xy'); # False

The starts-with and ends-with methods check the beginning and the end of a string:

say 'Raku'.starts-with('Ra'); # True
say 'Raku'.ends-with('ku');   # True

When you need the exact position of a substring, use index. It returns the index of the first match, counting from zero:

say 'Raku'.index('k'); # 2

If the substring is not found, index returns Nil rather than a number:

say 'Raku'.index('z'); # Nil

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Strings   |   Quiz — index


💪 Or jump directly to the exercises in this section.