学習したことのアウトプットとして
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
※補足等があればコメントいただけると幸いです