ruby字符串的encoding,force_encoding,encode,encode!转码(编码转换)

ruby字符串的encoding,force_encoding,encode,encode!转码(编码转换)

ruby1.9开始对字符串编码支持已经比较完善,我们可以直接通过使用String类的实例方法encoding,force_encoding, encode, encode!进行相关的编码操作。


encoding

ruby1.9中为每个字符串对象增加了encoding信息

?
1
2
3
1 . 9 .3p392 : 001 > '我还是不懂' .encoding
  => #<Encoding:UTF-8>
1 . 9 .3p392 : 002 >


force_encoding

某些情况下这个附加编码信息可能不正确我们可以修正它

?
1
2
3
4
5
6
7
8
9
10
11
12
13
1 . 9 .3p392 : 011 > x= '我还是不懂'
  => "我还是不懂"
1 . 9 .3p392 : 012 > x.encoding
  => #<Encoding:UTF-8>
1 . 9 .3p392 : 013 > x.bytes.to_a
  => [ 230 , 136 , 145 , 232 , 191 , 152 , 230 , 152 , 175 , 228 , 184 , 141 , 230 , 135 , 130 ]
1 . 9 .3p392 : 014 > x.force_encoding 'gbk'
  => "\x{E688}\x{91E8}\x{BF98}\x{E698}\x{AFE4}\x{B88D}\x{E687}\x82"
1 . 9 .3p392 : 015 > x.encoding
  => #<Encoding:GBK>
1 . 9 .3p392 : 016 > x.bytes.to_a
  => [ 230 , 136 , 145 , 232 , 191 , 152 , 230 , 152 , 175 , 228 , 184 , 141 , 230 , 135 , 130 ]
1 . 9 .3p392 : 017 >

注意:force_encoding方法只是改变了字符串对象的编码信息,并没有改变字符串对象实际存储的内容。


encode、encode!

在ruby1.9之前如我我们需要编码转换则需要使用一些外部库, 现在我们可以直接使用String对象的实例方法encode, encode!进行操作

?
1
2
3
4
5
encode(encoding [, options] ) → str click to toggle source
encode(dst_encoding, src_encoding [, options] ) → str
encode([options]) → str
encode!(encoding [, options] ) → str click to toggle source
encode!(dst_encoding, src_encoding [, options] ) → str

详细的api请参考这里


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1 . 9 .3p392 : 009 > x= '我还是不懂'
  => "我还是不懂"
1 . 9 .3p392 : 010 > x.encoding
  => #<Encoding:UTF-8>
1 . 9 .3p392 : 011 > x.bytes.to_a
  => [ 230 , 136 , 145 , 232 , 191 , 152 , 230 , 152 , 175 , 228 , 184 , 141 , 230 , 135 , 130 ]
1 . 9 .3p392 : 012 > y=x.encode 'gbk' , 'utf-8'
  => "\x{CED2}\x{BBB9}\x{CAC7}\x{B2BB}\x{B6AE}"
1 . 9 .3p392 : 013 > y.encoding
  => #<Encoding:GBK>
1 . 9 .3p392 : 014 > y.bytes.to_a
  => [ 206 , 210 , 187 , 185 , 202 , 199 , 178 , 187 , 182 , 174 ]
1 . 9 .3p392 : 015 > x.encode! 'gbk' , 'utf-8'
  => "\x{CED2}\x{BBB9}\x{CAC7}\x{B2BB}\x{B6AE}"
1 . 9 .3p392 : 016 > x.encoding
  => #<Encoding:GBK>
1 . 9 .3p392 : 017 > x.bytes.to_a
  => [ 206 , 210 , 187 , 185 , 202 , 199 , 178 , 187 , 182 , 174 ]
1 . 9 .3p392 : 018 >

可以看到encode改变了编码信息同时也改变了字符串对象存储的内容


总结

  • encdoing用来查看字符串的编码信息。
  • force_encoding用来修正字符串编码信息,注意是修正。
  • encodeencode!用来转码字符串。


转自:http://www.mojidong.com/ruby/2013/05/19/ruby-string-encoding/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的字符串编码和解码可以通过encode()和decode()方法来实现。这两个方法都是字符串对象的方法,用于将字符串从一种编码格式转换为另一种编码格式。 encode()方法可以将字符串编码为指定的编码格式,其语法为: ```python string.encode(encoding=编码格式, errors=错误处理) ``` 其中,encoding参数是必须的,用于指定要使用的编码格式,errors参数是可选的,用于指定错误处理方式。如果不指定errors参数,则默认使用strict模式,即如果存在无法编码的字符,则会抛出UnicodeError异常。 例如,将一个字符串编码为UTF-8格式: ```python string = "Hello World" encoded_string = string.encode(encoding="utf-8") print(encoded_string) ``` 输出结果为:b'Hello World'。其中,b表示这是一个字节串对象,而不是字符串对象。 decode()方法可以将字节串解码为指定的编码格式,其语法为: ```python bytes.decode(encoding=编码格式, errors=错误处理) ``` 其中,encoding参数是必须的,用于指定要使用的编码格式,errors参数是可选的,用于指定错误处理方式。如果不指定errors参数,则默认使用strict模式,即如果存在无法解码的字节,则会抛出UnicodeError异常。 例如,将一个UTF-8格式的字节串解码为字符串: ```python bytes_string = b'Hello World' decoded_string = bytes_string.decode(encoding="utf-8") print(decoded_string) ``` 输出结果为:Hello World。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值