java笔记-如何判断字符串的编码?
2018-02-23 14:26
316 查看
由于字符串的编码存在着多种可能,如果没有知名其编码格式,那么就只能一个个去测试,代码如下:
这个方法是用encode这个参数对str这个字符串进行编码,其官方的注释如下:
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
如果不给定字符编码,则按照默认的的字符编码进行编码,如果你的eclipse设置默认的编码是UTF-8就按照UTF-8进行编码;在编码之前,这个str变量在内存是以Unicode编码存在的。
这个方法是用encode这个参数对str.getBytes(encode)这个字节数组进行解码,其官方的注释如下:
Constructs a new String by decoding the specified array of bytes using the specified charset. 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.
如果不给定字符编码,则按照默认的的字符编码进行解码,如果你的eclipse设置默认的编码是UTF-8就按照UTF-8进行解码;在解码之后,这个str变量在内存中就以Unicode编码存在的。
在这里,Unicode编码作为各种编码之间的中转。以UTF-8为例:
UTF-8 —> Unicode —> UTF-8
String str = "测试字符串"; String encode = "UTF-8"; try { if (str.equals(new String(str.getBytes(encode), encode))) { System.out.println(encode); } } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } encode = "ISO-8859-1"; try { if (str.equals(new String(str.getBytes(encode), encode))) { System.out.println(encode); } } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } ...
补充
str.getBytes(encode)
这个方法是用encode这个参数对str这个字符串进行编码,其官方的注释如下:
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
如果不给定字符编码,则按照默认的的字符编码进行编码,如果你的eclipse设置默认的编码是UTF-8就按照UTF-8进行编码;在编码之前,这个str变量在内存是以Unicode编码存在的。
new String(str.getBytes(encode), encode)
这个方法是用encode这个参数对str.getBytes(encode)这个字节数组进行解码,其官方的注释如下:
Constructs a new String by decoding the specified array of bytes using the specified charset. 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.
如果不给定字符编码,则按照默认的的字符编码进行解码,如果你的eclipse设置默认的编码是UTF-8就按照UTF-8进行解码;在解码之后,这个str变量在内存中就以Unicode编码存在的。
为什么又要编码又要解码?
这里我说下自己粗糙的见解,虽然字符串在内存以Unicode编码存在,但是为了有效提高空间利用率,在保存到硬盘或者将其在网络间传输时,会对字符串进行编码,这样可以减少占用的资源;当我们读取这个字符串时,再将之解码到内存中,方便进行各种操作。在这里,Unicode编码作为各种编码之间的中转。以UTF-8为例:
UTF-8 —> Unicode —> UTF-8
相关文章推荐
- JAVA如何判断字符串编码
- JAVA如何判断字符串编码
- PHP学习笔记之字符串编码的转换和判断
- 如何判断一个字符串的编码类型?
- java 判断字符串什么编码类型
- 回复:String中如果有中文,如何保证length()返回的长度 在 Java 中字符串是以 Unicode 方式编码的,所以,无论中英文都算的长度为 1。根据你的描述,估计是想要取得英文长度为 1 中文长度为 2 的总长度吧。这个,理论上可以构造 An
- java控制台程序如何判断String字符串中只输入了一个回车
- 非常一般的 JAVA 的学习笔记 —— 字符串编码小觑
- java怎么判断byte[]的原字符串的编码?
- java 判断字符串什么编码类型
- java中如何从字符串中删选中文字符(GBK编码和UTF-8编码)
- java 如何判断一个字符串是数字
- java可供判断某字符串是什么编码的一行代码
- java 判断字符串什么编码类型
- Java中如何判断一个字符串的格式
- 在java如何判断字符串有多少中文字
- java中如何判断一个字符串中含有字母或数字
- java如何做到判断一个字符串是否是数字
- JAVA如何判断两个字符串是否相等
- 收藏 java 如何判断txt的编码字符集