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 1 year has passed since last update.

対象となる文字列からn番目の文字を削除するメソッド

Last updated at Posted at 2022-06-13

任意の文字列の先頭からn番目の文字を削除するメソッドを紹介します。

例えば下記のように郵便番号111-1111を入力するが間のハイフンを削除して出力したいときなど削除したい文字が何番目にあるかわかっている時などを想定した時

def address(number)
  number.slice!(3)
  puts number                 #1111111が出力されて"-"が削除されている
end

number = "111-1111"
address(number)

slice!の後ろの()内は配列と同じように0から始まる数字を入れます!

slice!メソッドは配列や文字列から指定した要素を削除して返します

0
0
2

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?