Java 输出指定编码的字符串
2013-06-19 22:30
211 查看
Java Sting类有个根据byte,字符编码来输出的构造函数。 以下为java文档中的解释。 public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException
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.
The behavior of this constructor when the given bytes are not valid in the given charset is unspecified. The
CharsetDecoderclass should be used when more control over the decoding process is required.
Parameters:
bytes- the bytes to be decoded into characters
charsetName- the name of a supported charsetThrows:
UnsupportedEncodingException- If the named charset is not supportedSince:JDK1.1
下面代码为转换的函数:
/*** * 按照指定编码转换字符串 * @param str 需要转换的字符串 * @param srcEncode 传递过来的字符串编码格式 * @param dstEncode 需要转换成的编码格式 * @return 指定编码格式的字符串 */ public String translate(String str, String srcEncode,String dstEncode) { String tempStr = ""; try { tempStr = new String(str.getBytes(srcEncode),dstEncode); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tempStr; }
相关文章推荐
- java程序,编写一个方法,输出在一个字符串中,指定字符串输出的次数
- JAVA 输出指定字符串所有排列组合
- 计算给定字符串中每个字符出现的次数,并以指定格式输出,例如aabbbc输出a2b3c1(java语言)
- 【Java】编写一个方法,输出在一个字符串中,指定字符串出现的次数
- java IO 指定输出文件的编码格式
- Java学习之使用集合实现对字符串中元素指定格式输出
- 浅谈java使用指定字符集编码,以及常见的字符集
- Java反转字符串和相关字符编码问题
- java如何输出数组里的字符串到控制台,需要借助for循环
- 华为编程题之五:输入一个正整数,并编码为字符串进行输出
- php使用mb_check_encoding检查字符串在指定的编码里是否有效
- 字符串截取指定长度字节数(Java方法实例,注:同样思路js也适用)
- java文件读写操作指定编码格式
- java字符串编码类型获取
- 用java将字符串中的数字输出
- java 获取字符串的编码
- java的几种流和字符串编码
- AS3.0的Socket通讯发送固定字符串(指定编码)
- Java Base64位编码与String字符串的相互转换,Base64与Bitmap的相互转换实例代码
- Java 基础 —— String中替换指定字符,字符种类判定,字符大小写转换,遍历字符串