LoginSignup
2
0

More than 3 years have passed since last update.

MacでNFDな文字列でもNFCにリネームしたい!

Posted at
path = "㌧㌦ベイビー.txt"

p nfd_path = path.unicode_normalize(:nfd)
p nfd_path.each_char.to_a
# => "㌧㌦ベイビー.txt"
# => ["㌧", "㌦", "ヘ", "゙", "イ", "ヒ", "゙", "ー", ".", "t", "x", "t"]

p nfc_path = nfd_path.unicode_normalize(:nfc)
p nfc_path.each_char.to_a
# => "㌧㌦ベイビー.txt"
# => ["㌧", "㌦", "ベ", "イ", "ビ", "ー", ".", "t", "x", "t"]

# ついでにnfkcも
p nkfc_path = nfd_path.unicode_normalize(:nfkc)
p nkfc_path.each_char.to_a
# => "トンドルベイビー.txt"
# => ["ト", "ン", "ド", "ル", "ベ", "イ", "ビ", "ー", ".", "t", "x", "t"]

# File.renameか何かリネームしてもよし
# File.rename(path, path.unicode_normalize(:nfc))

ちゃんと知りたい人は

Unicode正規化 https://ja.wikipedia.org/wiki/Unicode%E6%AD%A3%E8%A6%8F%E5%8C%96

2
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
2
0