String str = " a b c d ";  如何把str 替换为 abcd  (即去掉\" 及 空格)  用较简洁的代码实现,另外也用正则表达式的方式实现下

解决方案 »

  1.   

                    String str = " a b c d ";
    str=str.replaceAll("\\s", "");
    System.out.println(str);
      

  2.   


    String a = " a b c d ";
    a = a.replaceAll("\\s*", "");


    System.out.println(a);
      

  3.   

    唉,我自作聪明问错问题了
             String password = "abcd";
            String newPass = "\"" + password + "\"";
            byte[] newUnicodePassword = newPass.getBytes("UTF-16LE");        //我要做的是把 newUnicodePassword 变成一个字附串,值等于原来那个  abcd
            //我的实现方式如下:
            String oldPass = new String(newUnicodePassword);
            oldPass.replaceAll("\\s+", "");
            System.out.println(oldPass);
            //上面的这种替换方法,输出的结果为  " a b c d "    (其中\" 也是在输出结果中)
      

  4.   

    不好意思又问错问题了,在楼上的倒数第三句,改为
    oldPass = oldPass.replaceAll("\\s+","");   运行结果是一样的
      

  5.   

    String str = " a b c d ";
    str=str.replace("", " ");
    System.out.println(str);