`
nnnnon
  • 浏览: 147826 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
中文匹配与中文转码
#coding:utf-8

require 'iconv'

class String
   def to_gb
     Iconv.conv("gb2312//IGNORE","UTF-8//IGNORE",self)
   end
   def utf8_to_gb
     Iconv.conv("gb2312//IGNORE","UTF-8//IGNORE",self)
   end
   def gb_to_utf8
     Iconv.conv("UTF-8//IGNORE","GB18030//IGNORE",self)
   end
   def to_utf8
     Iconv.conv("UTF-8//IGNORE","GB18030//IGNORE",self)
   end
end


if  /[\u4e00-\u9fa5]+/ =~ "马立军"  then
     puts "是中文".to_gb  #cmd窗口可以显示
     puts "马立军 S0810040 打".gsub!(/[\u4e00-\u9fa5]+/ ,"").strip.length
else
     puts "no"
end
Global site tag (gtag.js) - Google Analytics