"你好".force_encoding("GBK") == "你好" #>> false
"Hello".force_encoding("GBK") == "Hello" #>> true
"你好".force_encoding("GBK") <=> "你好" #>> 1
"Hello".force_encoding("GBK") <=> "Hello" #>> 0
s1 = "a" + "b"
s2 = :a.to_s + "b"
s3 = :a.to_s + "中文"
s1.encoding.name #>> "UTF-8"
s2.encoding.name #>> "US-ASCII"
s3.encoding.name #>> "UTF-8"
irb:
puts "hello".encoding #>> 'UTF-8'
ruby test.rb:
puts "hello".encoding #>> 'US-ASCII'
Ruby 1.9 String Encoding (WIP)
最新推荐文章于 2025-05-25 22:40:06 发布
本文探讨了字符串在不同编码格式间的转换方法,特别是针对中文字符在GBK与UTF-8之间的转换特性,并展示了Ruby语言中字符串编码操作的具体应用实例。
282

被折叠的 条评论
为什么被折叠?



