0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby で文字列を置換する方法 gsub

Last updated at Posted at 2020-11-19

Rubyで文字列を置換:Stringクラスのgsub

Ruby初学者である自分のための、Ruby 2.7.0についての記事です。

文字列の一部を特定の文字列に置き換える方法です。

  • 基本的な使い方 gsub(pattern, replace)

例えば、"def"を"!!"に置き換えたい場合

# 公式ドキュメントより
'abcdefg'.gsub(/def/, '!!')  # => "abc!!g"

# 変数を使う場合(自分の解釈)
S = "abcdefg" # 文字列を変数Sに代入します
puts S.gsub(/def/, '!!') # => abc!!g

一番簡単な部分だけ抜粋しています。詳細は公式ドキュメントを確認します。
https://docs.ruby-lang.org/ja/latest/method/String/i/gsub.html

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?