The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

types.string

Provides functions for creating and manipulating string-like values.

substring

Description

Returns ``n`` characters from ``str`` beginning at ``position`` (first character in a string is ``0``).

Without ``n`` will return from ``position`` to the end of the original string.

A negative value for ``n`` will return from ``position`` until ``|n| - 1`` characters prior to the end of the string (``n = -1`` would have the same effect as omitting ``n``).

Usage

<str> <position> [<n>]

Examples

    :emphasize-lines: 2

    (substring "The quick brown fox ..." 4 5)
    "quick"

index

Description

Returns the starting position(s) in a list of all occurrences of the substring ``match`` in ``str``. If ``match`` does not exist anywhere in ``str`` then an empty list is returned.

Usage

<str> <match>

Examples

    :emphasize-lines: 2,5

    (index "The quick brown fox ..." "fox")
    (16)

    (index "The quick brown fox ..." "o")
    (12 17)

index-n

Description

Returns the nth (from 1) starting position of the substring ``match`` in ``str``.

If there are no occurrences of ``match`` in ``str``, or there are less than ``n``, nothing is returned.

Usage

Examples

    :emphasize-lines: 2

    (index-n "This string has three occurrences of the substring \"str\" in it." "str" 2)
    44