10
11

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 5 years have passed since last update.

RubyのString#deleteの意外な機能

Last updated at Posted at 2016-02-09

Stringクラスのインスタンスメソッド、「delete」
その名の通り、レシーバの文字列から引数の文字列を消すメソッド。

a = '12345'
puts a.delete('3')

# => 1245

単語単位ではなく、文字で消すのが注意すべきところだが、、、

a = '12345'
puts a.delete('35')

# => 124

ここまではまぁ大丈夫。

しかし、こんな引数の書き方が存在する。

a = '12345'
puts a.delete('3-5')

これを実行すると。。。

# => 12

範囲もかけるらしい。。。

いやいや。いらないいらない。笑

20160826更新

こんなのもできた


# 改行して文字列を作ると、strに¥nと空白が入ってしまう
str = "http://hogehoge.com
        /fuga
        /piyo".delete("¥n| ") # or

10
11
7

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
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?