class String
Constants
- CELLPHONE_NUM_REGEX
- CHINESE_CHARACTER_REGEX
- EMAIL_ADDR_REGEX
SINGLE_BYTE_REGEX = /A+z/ def is_single_byte_char?
!!(SINGLE_BYTE_REGEX =~ self.strip)
end
Notice: In mysql db with utf8, varchar(4) means 4 chinese characters or 4 numbers or 4 english letters. So this method is NOT used for valid whether the string can be stored to mysql db with utf8. return char size of the string, with 2 for each chinese characters and 1 for others.
MULTI_BYTE_REGEX = /[^u0000-u00ff]/ def char_size
self.gsub(MULTI_BYTE_REGEX, '12').length
end
Public Instance Methods
is_cellphone_num?()
click to toggle source
# File lib/patch_utils/string.rb, line 31 def is_cellphone_num? !!(CELLPHONE_NUM_REGEX =~ self.strip) end
is_chinese_character?()
click to toggle source
# File lib/patch_utils/string.rb, line 26 def is_chinese_character? !!(EMAIL_ADDR_REGEX =~ self.strip) end
is_email?()
click to toggle source
# File lib/patch_utils/string.rb, line 21 def is_email? !!(EMAIL_ADDR_REGEX =~ self.strip) end