LoginSignup
0
0

More than 1 year has passed since last update.

対象の文字列から指定した文字を取り出すscanメソッド

Posted at

対象となる文字列や、文字列を代入した変数から指定した文字を取り出し、その数を出力する方法を記述していきます。

comment = "great good bad good good"

例として上記のような形でcomment変数に入ったgoodの数を知りたい時は次のようにscanメソッドが使用できます。

comment.scan("good").length

=>3

scanメソッドは文字列を配列の形で取り出します。

comment.scan("good")

=> ["good", "good", "good]

なのでlengthを用いて要素の数を取り出しています。

0
0
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
0
0