ruby1.9.2 String中的 encode, encode!, force_encoding.
根据 http://yugui.jp/articles/850 这上面说的。
For aString,String#encodekeeps the characters but changes the encoding in which the characters are encoded.#encodeis not destructive but#encode!is the destructive version. The byte representation of a character is depend on encoding. So#encodeand#encode!generally change the byte representation of the string.
#force_encodingin contrarily keeps byte representation but changes characters. After#force_encoding, sometimesthe string become invalid as a character sequence.
In other words,#encodetreats aStringobject as a character sequence but#force_encodingtreats 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 阅读(775) 评论(0) 收藏 举报
浙公网安备 33010602011771号