LoginSignup
0
0

More than 3 years have passed since last update.

複数の文字列から特定の文字が含まれた文字列を抽出する

Posted at

目的はタイトル通り

ですが、サブの目的として”もし特定の文字が含まれた文字列がなければ結果としてNoneを出力する”ことと、文字列は入力によって取得することとします。

1.まず入力される文字列の回数をnに代入します
2.次に入力される特定の文字をgに代入します
3.Noneを出力するために、後々入力された文字列を格納する配列をs_arrayに代入していきます
4.n回の処理を行うためn.times do〜endを記述します
5.処理内容としてまず審査する文字列をsに代入します
6.sにgが含まれているかをs.include?(g)で審査し、trueであれば下記の処理を行います
7.s_arrayにsを格納し、sを出力します
8.ここまでの処理で、もしs_arrayが空であった場合Noneを出力します

n = gets.chomp.to_i
g = gets.chomp

s_array = []

n.times do
    s = gets.chomp

    if s.include?(g)
        s_array.push(s)
        puts s
    end
end

if s_array.empty?
    puts 'None'
end

もしかしたらもっと効率的なやり方があるかもしれませんが、少しでも駆け出しエンジニアの方々の参考になれば幸いです。
では。

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