Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
たとえば、1 は 2 つのダッシュで構成され、8 は 7 つのダッシュで構成されます。このように、この文字列メッセージを入力として受け取り、対応する値を数値で返す関数を作成します。この数は、文字列メッセージ内のダッシュの数です。
文字列にはcountメソッドがあります:
count
"abc--de-f-".count('-') #=> 4
入力文字列からダッシュのみを含む文字列を取得し、その文字列の長さを確認します。
dash_string = input_string.gsub(/[^-]/, '') number = dash_string.length
その場合、ダッシュのない文字列は -1 になることに注意してください。