\u4e2d\u56fd\ java代码里怎么转换成中文呢? \u4e2d\u56fd\ java代码里怎么转换成中文呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String str = new String("\u4e2d\u56fd"); System.out.println(str); 楼主的意思是把 \u4e2d\u56fd\ java 的Unicode码转化? 这个可以实现:Java codeString str = new String("\u4e2d\u56fd"); System.out.println(str); String str = "\u4e2d\u56fd";System.out.println(str); String s = "\\u4e2d\\u56fd"; StringBuffer sb = new StringBuffer(); Pattern p = Pattern.compile("(?i)\\\\u([\\da-f]{4})"); Matcher m = p.matcher(s); while(m.find()) { m.appendReplacement(sb, Character.toString((char)Integer.parseInt(m.group(1), 16))); } m.appendTail(sb); System.out.println(sb.toString()); 这样可以实现,但"\u4e2d\u56fd"是放在一个String变量里面的,而我用String a=new String(a);a就不是中文了??? 装一个eclipse插件 http://www.lifeispig.cn/article.asp?id=116 看一下这个插件吧..还好用.我一直是用他做国际化.. 没有问题啊,怎么可能不行呢?那还真邪门了呵呵public static void main(String args[]){ String str1 = "\u4e2d\u56fd"; String str2 = new String("\u4e2d\u56fd"); System.out.print(str1 +","+ str2); }中国,中国 // 这样子是实现了吗?String str = new String("\u4e2d\u56fd");System.out.println(str);// 要这样子的话// 下面这样子都会出来"中国", 根本连 new String 都不用String str = "\u4e2d\u56fd"; System.out.println(str);\uxxxx 刚好也是 java 定义字符串内容时的一种转义形式 任意编码转换 ByteBuffer bbuf = ByteBuffer.wrap("\u4e2d\u56fd".getBytes("utf-16")); Charset cs = Charset.forName("utf-16"); CharsetDecoder decoder = cs.newDecoder(); CharBuffer cbuf = decoder.decode(bbuf); cs = Charset.forName("gbk"); CharsetEncoder encoder = cs.newEncoder(); ByteBuffer outbuffer = encoder.encode(cbuf); String str1 = new String(outbuffer.array(), "gbk"); System.out.println(str1); 赫赫,那你就用最傻的办法: String content = new String("\u4e2d\u56fd".getBytes("gbk"),"gbk"); System.out.println(content);楼上的都说了String str = new String("\u4e2d\u56fd"); System.out.println(str); 也行,因为中文操作系统 JVM的默认编码格式就是GBK.如果你是英文操作系统,就按我上面那样写全了。 你那两行代码都是可以的但我这边很怪String str的变量的值打印出来是 \u4e2d\u56fd但想把它转换成中文的“中国”详细如下:有一个client.properties里写有name=中国然后public static Hashtable properties=null;StringUtil st=new StringUtil();properties=st.loadProperties("/client.properties",'=');String str1=(String)properties.get("name");打印的str1却是"\u4e2d\u56fd",而不是"中国"************String str2 = "\u4e2d\u56fd";String str3 = new String(str2); System.out.println("======str3========"+str3);这样又可以 用这个: String str1=(String)properties.get("name"); StringBuffer sb = new StringBuffer(); Pattern p = Pattern.compile("(?i)\\\\u([\\da-f]{4})"); Matcher m = p.matcher(str1); while(m.find()) { m.appendReplacement(sb, Character.toString((char)Integer.parseInt(m.group(1), 16))); } m.appendTail(sb); str1 = sb.toString(); 我用的是jdk 1.3,并不支持jdk 6.0的APTPattern Matcher 1.3 有不熟了看这个能不能用 String str1=(String)properties.get("name"); StringBuffer sb = new StringBuffer(str1); int pos; while ((pos=sb.indexOf("\\u"))>-1) { String tmp = sb.substring(pos, pos+6); sb.replace(pos, pos+6, Character.toString((char)Integer.parseInt(tmp.substring(2), 16))); } str1 = sb.toString(); System.out.println(str1); 一般在ECLIPSE导入插件就行了。 对,在Eclipse中从网上下一个插件,就是在Application.properts文件中就可以把汉字变成编码。。然后就读取该文件 就可了 还有就是在Jbuidel中也可以将汉字转换成编码 换一种思路,怎么把以下的第二行代码变成String str3 = new String("\u4e2d\u56fd"); 可以用java中的转义字符String str3 = new String("\(这里应该怎么写呢?)str2"); String str2 = "\u4e2d\u56fd"; String str3 = new String(str2); System.out.println("======str3========"+str3); 答:一点都不奇怪.楼主忽略了:你是从属性文件中读取\u4e2d\u56fd的.当你读入String str1中时,str1实际上放的是:\\u4e2d\\u56fd,这就是你看到的结果.由于你的JDK1.3 ,我就用JDK1.0来写一个转换方法如下(保证楼主成功):public static String convert(String s) { StringBuffer sb = new StringBuffer(); int i=-1; int pos=0; while((i=s.indexOf("\\u",pos))!=-1) { sb.append(s.substring(pos,i)); if(i+5<s.length()) { pos=i+6; sb.append((char)Integer.parseInt(s.substring(i+2,i+6),16)); }//if }//while sb.append(s.substring(pos)); return sb.toString(); }若你的属性文件中放的是:name=\u4e2d\u56fdabc则你使用:public static Hashtable properties=null; StringUtil st=new StringUtil(); properties=st.loadProperties("/client.properties",'='); String str1=convert( (String)properties.get("name") ); System.out.println(str1);运行结果就是:中国abc 怎么让我从Java API继承的类实例化时调用的是自己写的ClassLoader 问个SWING问题 初次structs,初次java,遇到问题,还望大家帮忙。 关于局部页面切换 如何在Java程序调用Vc、C和Java编译器的问题 请问怎样给一个数组的每一个元素一次性地赋同样的值 java 中的 ^ 是什么意思? 插入符问题 顶者有分 java导出乱码的问题 如何用JDBC连接DOMINO数据库? ThreadLocal 为何要定义成 final ? ? ? JMETER相关:JMETER可以做到WAS里的按比例访问进行负载测试吗?
System.out.println(str);
Java codeString str = new String("\u4e2d\u56fd");
System.out.println(str);
String s = "\\u4e2d\\u56fd";
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("(?i)\\\\u([\\da-f]{4})");
Matcher m = p.matcher(s);
while(m.find()) {
m.appendReplacement(sb, Character.toString((char)Integer.parseInt(m.group(1), 16)));
}
m.appendTail(sb);
System.out.println(sb.toString());
String a=new String(a);a就不是中文了???
没有问题啊,怎么可能不行呢?那还真邪门了呵呵public static void main(String args[]){
String str1 = "\u4e2d\u56fd";
String str2 = new String("\u4e2d\u56fd");
System.out.print(str1 +","+ str2);
}
中国,中国
// 这样子是实现了吗?
String str = new String("\u4e2d\u56fd");
System.out.println(str);// 要这样子的话
// 下面这样子都会出来"中国", 根本连 new String 都不用
String str = "\u4e2d\u56fd";
System.out.println(str);\uxxxx 刚好也是 java 定义字符串内容时的一种转义形式
ByteBuffer bbuf = ByteBuffer.wrap("\u4e2d\u56fd".getBytes("utf-16"));
Charset cs = Charset.forName("utf-16");
CharsetDecoder decoder = cs.newDecoder();
CharBuffer cbuf = decoder.decode(bbuf);
cs = Charset.forName("gbk");
CharsetEncoder encoder = cs.newEncoder();
ByteBuffer outbuffer = encoder.encode(cbuf);
String str1 = new String(outbuffer.array(), "gbk");
System.out.println(str1);
System.out.println(content);
楼上的都说了
String str = new String("\u4e2d\u56fd");
System.out.println(str);
也行,因为中文操作系统 JVM的默认编码格式就是GBK.
如果你是英文操作系统,就按我上面那样写全了。
有一个client.properties里写有
name=中国然后
public static Hashtable properties=null;
StringUtil st=new StringUtil();
properties=st.loadProperties("/client.properties",'=');String str1=(String)properties.get("name");打印的str1却是"\u4e2d\u56fd",而不是"中国"************String str2 = "\u4e2d\u56fd";
String str3 = new String(str2);
System.out.println("======str3========"+str3);
这样又可以
Pattern p = Pattern.compile("(?i)\\\\u([\\da-f]{4})");
Matcher m = p.matcher(str1);
while(m.find()) {
m.appendReplacement(sb, Character.toString((char)Integer.parseInt(m.group(1), 16)));
}
m.appendTail(sb); str1 = sb.toString();
Pattern
Matcher
StringBuffer sb = new StringBuffer(str1);
int pos;
while ((pos=sb.indexOf("\\u"))>-1) {
String tmp = sb.substring(pos, pos+6);
sb.replace(pos, pos+6, Character.toString((char)Integer.parseInt(tmp.substring(2), 16)));
}
str1 = sb.toString();
System.out.println(str1);
就可以把汉字变成编码。。然后就读取该文件 就可了
还有就是在Jbuidel中也可以将汉字转换成编码
String str2 = "\u4e2d\u56fd";
String str3 = new String(str2);
System.out.println("======str3========"+str3);
由于你的JDK1.3 ,我就用JDK1.0来写一个转换方法如下(保证楼主成功):public static String convert(String s)
{
StringBuffer sb = new StringBuffer();
int i=-1;
int pos=0;
while((i=s.indexOf("\\u",pos))!=-1)
{
sb.append(s.substring(pos,i));
if(i+5<s.length())
{
pos=i+6;
sb.append((char)Integer.parseInt(s.substring(i+2,i+6),16));
}//if
}//while
sb.append(s.substring(pos));
return sb.toString();
}
若你的属性文件中放的是:name=\u4e2d\u56fdabc
则你使用:
public static Hashtable properties=null;
StringUtil st=new StringUtil();
properties=st.loadProperties("/client.properties",'='); String str1=convert( (String)properties.get("name") );
System.out.println(str1);
运行结果就是:
中国abc