String x="fsdaf\r\n24342";
x=x.replaceAll("\\r","");
如果要去掉空格,\t,\r,\n这些所有东西就用
x=x.replaceAll("[\\s]*","");

解决方案 »

  1.   

    是不是啊?replace哪能用双引号作参数呀?
      

  2.   

    那么我改一改,
    x=x.replace('4','8');
    也无效果,x还是原来的x,怎么回事?
      

  3.   

    这应该是个SCJP程序员考试的讲义,可是不记不清楚了
      

  4.   

    String x="fsdaf\r\n24342";
    x=x.replaceAll("\\r","");
    如果要去掉空格,\t,\r,\n这些所有东西就用
    x=x.replaceAll("[\\s]*","");
    你看清楚哦
    是replaceAll
    而且都是双引号
      

  5.   

    啊;我把题目问个清楚,我的意思是想用replace把一个字符串里面的某个字符全部替换成另一个字符,但是替换通过编译,却在执行的时候仍然没有替换.我的JB中没有replaceAll方法,楼上的兄弟,用的不是标准的JDK,你用了什么开发包,导入了replaceAll方法?
    JDK2A的帮助文档里面也没有这个方法的,请楼上验证.
      

  6.   

    晕,我无语了,楼主的jdk版本肯定是低于1。4
    你看我的java 1.4的doc
    replaceAll
    public String replaceAll(String regex,
                             String replacement)Replaces each substring of this string that matches the given regular expression with the given replacement. 
    An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)Parameters:
    regex - the regular expression to which this string is to be matched 
    Returns:
    The resulting String 
    Throws: 
    PatternSyntaxException - if the regular expression's syntax is invalid 
    NullPointerException - if regex is null
    Since: 
    1.4 
    See Also:
    Pattern
      

  7.   

    明白了,一方面保存文件与时间的关系.此外: x="kkkklllkkk";x.replace('l','m');
    //x的值:kkkkklllkkky=x.replace('l','m');
    //x的值:kkkkklllkkk, y的值:kkkkkmmmkkk