1
3

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で7桁の郵便番号を3桁+4桁に分割する方法

Posted at

例えば、DBに郵便番号を7桁の文字列で保存しているけど、画面などに出力する際には分けたいときなど。
separator となる文字を挿入することで、split できるようにしています。

zip_code = 'xxxyyyy'
zip_code.insert(3, '-').split('-') # '-'じゃなくてもよい

もっと効率的な方法があれば知りたいです。

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?