一、字符串转换编码
public static String translate(){ String str="你好,我是喵喵.hello,I'm Delia."; String result = ""; try{ result = new String(str.getBytes("ISO-8859-1"),"GBK");//把字节数组转为字符串,第一个参数是字节数组,第二个参数是字符编码。 result = result.trim();//去掉字符串首尾空白 System.out.println(result); result = new String(str.getBytes("GBK"),"UTF-8");//把字符串转换成使用指定的字符集的字节序列,并将结果存储到一个新的字节数组 result = result.trim();//去掉字符串首尾空白 System.out.println(result); }catch (Exception e){ e.printStackTrace(); } return result; }
输出://String(byte bytes[], String charsetName)
//把字节数组转为字符串,第一个参数是字节数组,第二个参数是字符编码。
//getBytes(String charsetName) //把字符串转换成使用指定的字符集的字节序列,并将结果存储到一个新的字节数组
??,?????.hello,I'm Delia.
���,����������.hello,I'm Delia.
二、字符串按空格分割
public static void testStringTokenizer() { StringTokenizer st = new StringTokenizer("Welcome to our country"); while (st.hasMoreTokens()){ System.out.println(st.nextToken()); } }