日期:2014-05-17  浏览次数:20613 次

转义符的问题,求大神!!
	public static void main(String[] args) {
String s = "\\u4e2d\\u56fd";
//s = s.replace("\\u", "\u");
s = s.replace("\\\\u", "\\u");
System.out.println(s);
}


我想把这 \\ 转成 \ 求助!

------解决方案--------------------
你这个可以实现你想要的达到的目的
------解决方案--------------------
菜蔬学谦,没看懂
------解决方案--------------------
可以在编译之前替换。
------解决方案--------------------
引用:
Java code?123456public static void main(String[] args) {    String s = "\\u4e2d\\u56fd";    //s = s.replace("\\u", "\u");    s = s.replace("\\\\u", "\\u");    System.out.println(s);}

我……

楼主是想把字符串的中文字符搞出来吧?
你可以逐个替换,先把中文字符找出来,再转义嘛!
我写了一个,楼主看看能不能帮上忙
思路:
先找到"\\u",然后找接下去的4个字符(因为中文占两个长度,相当于4位16进制),然后转换成字符。


            String s = "China\\u4e2d+\\u56fdRMB";
            StringBuilder sbRet = new StringBuilder(s.length());  // 存放结果的
            for (int i = 0; i < s.length(); i++) {
                if ('\\' == s.charAt(i)) {         // 把"\\uXXXX"的字符找出来,转义
                    if ((i + 1 < s.length()) && 'u' == s.charAt(i + 1)) {
                        int start = i + 1 + 1;
                        int end = start + 4;
                        if (end <= s.length()) {
                            String strHex = s.substring(start, end);
                            sbRet.append((char) Integer.parseInt(strHex, 16));
                            i = end - 1;
                        }
                    }
            &n