ruby1.9.2 String中的 encode, encode!, force_encoding.
根据 http://yugui.jp/articles/850 这上面说的。
For aString
,String#encode
keeps the characters but changes the encoding in which the characters are encoded.#encode
is not destructive but#encode!
is the destructive version. The byte representation of a character is depend on encoding. So#encode
and#encode!
generally change the byte representation of the string.
#force_encoding
in contrarily keeps byte representation but changes characters. After#force_encoding
, sometimesthe string become invalid as a character sequence.
In other words,#encode
treats aString
object as a character sequence but#force_encoding
treats it as a bytesequence.
对于String, encode方法 保留字符, 但是当转码成功的时候改变编码,encode! 是 encode 的强制版。
每个字符的字节是根据编码来产生。也就是说,encode 和encode! 通常改变字符串的字节。
相反地,force_encoding方法 保留字节并且可能改变字符,在force_encoding后,一些字符会变成无效的字符序列。
一句话,encode 以字符序列的方式处理 String对象,而force_encoding 以字节序列的方式。
不知道理解的正确不正确,望高手指点。
posted on 2011-09-18 17:07 Eric812048774 阅读(772) 评论(0) 编辑 收藏 举报