LoginSignup
16
16

More than 5 years have passed since last update.

Ruby で手っ取り早く半角文字列を全角文字列に変換する.

Posted at

moij gem を使います.

インストール

$ gem install moji

実行例

"foo""foo" に変換します.

irb(main):001:0> require 'moji'
=> true

irb(main):002:0> Moji.han_to_zen('foo'.encode('utf-8'))
=> "foo"

String#encode で変換後の文字列エンコーディングを設定しておくのがポイントです. この例では utf-8 の全角文字に変換しています.ただ単に 'foo' としただけでは,us-ascii なので相当する全角文字がなく変換に失敗します.

16
16
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
16
16