LoginSignup
14
15

More than 5 years have passed since last update.

ある文字列がマルチバイト文字を含んでいるか

Last updated at Posted at 2012-08-10

String#bytesでとりだして上位ビットが1であるかどうかを見る

# -*- coding:utf-8 -*-

def has_mb?(str)
  str.bytes do |b|
    return true if  (b & 0b10000000) != 0
  end
  false
end

p has_mb?("this is 日本語") # => true
p has_mb?("this isn't nihongo!") # => false
14
15
1

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
14
15