Java之byte、char和String类型相互转换

 package basictype;

 /**
* byte、char和String类型相互转换
*/
public class CHJavaType {
public static void main(String[] args) {
String string = "abcd"; // String转char[]
char[] chars = string.toCharArray();
for (char c : chars) {
System.out.println(c);
}
// char[]转String
System.out.println("String.valueOf:" + String.valueOf(chars));
System.out.println("new String:" + new String(chars)); byte[] bytes = {};
try {
// String转byte[]
bytes = string.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
} // byte[]转String
System.out.println(new String(bytes));
}
}
上一篇:Java解析XML文档(简单实例)——dom解析xml


下一篇:XML文档的生成和解析操作方法