简体   繁体   English

使用byte []和charset的构造函数创建字符串对象

[英]String object creation using constructor of byte[] and charset

I am creating a String object using new String(byte[], Charset) where Charset = UTF8 and byte[] is encoded into ISO-8859-1 . 我正在使用new String(byte[], Charset)创建一个String对象new String(byte[], Charset)其中Charset = UTF8byte[]被编码为ISO-8859-1 I have tested with normal characters and it worked. 我已经测试了正常字符,并且可以正常工作。 But not clear if it can create any problem. 但不清楚是否会造成任何问题。

Pleas suggest. 建议。

new String(byteArray,Charset.forName("UTF-8"));

Read the API documentation regarding the constructor. 阅读有关构造函数的API文档。 According to that you will not get any issues even if you get a corrupted byte array. 因此,即使字节数组损坏,也不会出现任何问题。

public String(byte[] bytes, Charset charset) 公共字符串(字节[]字节,字符集字符集)

Constructs a new String by decoding the specified array of bytes using the specified charset. 通过使用指定的字符集解码指定的字节数组来构造新的String。 The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array. 新String的长度是字符集的函数,因此可能不等于字节数组的长度。 This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. 此方法始终使用此字符集的默认替换字符串替换格式错误的输入和不可映射的字符序列。 The CharsetDecoder class should be used when more control over the decoding process is required 当需要对解码过程进行更多控制时,应使用CharsetDecoder类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM