1
1

More than 1 year has passed since last update.

[Ruby] Stringクラスのindexメソッド

Posted at

学習したことのアウトプットとして

indexメソッド

Rubyの組み込みライブラリ
index(pattern, pos = 0) -> Integer | nil
文字列のインデックス pos から右に向かって pattern を検索し、最初に見つかった部分文字列の左端のインデックスを返す。
見つからなければ nil を返す。

  • 引数 pattern は探索する文字列または正規表現で指定
  • 引数 pos が負の場合、文字列の末尾から数えた位置から探索する、0の場合省略可

例)

 "astrochemistry".index("str")         # => 1
 "regexpindex".index(/e.*x/, 2)        # => 3
 "character".index(?c)                 # => 0

 "foobarfoobar".index("bar", 6)        # => 9
 "foobarfoobar".index("bar", -6)       # => 9

※補足等があればコメントいただけると幸いです

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1