perl-5.38.0
index STR,SUBSTR,POSITION
index STR,SUBSTR

The index function searches for one string within another, but without the wildcard-like behavior of a full regular-expression pattern match. It returns the position of the first occurrence of SUBSTR in STR at or after POSITION. If POSITION is omitted, starts searching from the beginning of the string. POSITION before the beginning of the string or after its end is treated as if it were the beginning or the end, respectively. POSITION and the return value are based at zero. If the substring is not found, index returns -1.

index 関数は ある文字列をもうひとつの文字列から検索しますが、 完全正規表現パターンマッチのワイルドカード的な振る舞いはしません。 STR の中の POSITION の位置以降で、最初に SUBSTR が見つかった位置を返します。 POSITION が省略された場合には、STR の最初から探し始めます。 POSITION が文字列の先頭より前、あるいは末尾より後ろを指定した場合は、 それぞれ先頭と末尾を指定されたものとして扱われます。 POSITION と返り値のベースは、0 です。 SUBSTR が見つからなかった場合には、index は -1 が返されます。

Find characters or strings:

文字や文字列を探すには:

    index("Perl is great", "P");     # Returns 0
    index("Perl is great", "g");     # Returns 8
    index("Perl is great", "great"); # Also returns 8

Attempting to find something not there:

ないものを探そうとすると:

    index("Perl is great", "Z");     # Returns -1 (not found)

Using an offset to find the second occurrence:

2 番目 の出現位置を探すためにオフセットを使うと:

    index("Perl is great", "e", 5);  # Returns 10