想问一下,java中字符串替换
String strTemp = "abcdefgsdfs";
想把f替换成空字符。
strTemp.replace('f',?);
请问空字符怎么写?

解决方案 »

  1.   

    用repleaceAll吧  这样可以用空字符串
      

  2.   

    replaceAll   
    一不小心打错字了
      

  3.   

    replaceAll是1.4后的,呵呵。注意
      

  4.   

    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
      

  5.   

    public  class strReplace{
      public static void main(String args[]){
      String str="abcdefgfgf";
      str=str.replace('f',' ');
      System.out.println(str);  }
    }
      

  6.   

    public  class strReplace{
      public static void main(String args[]){
      String str="abcdefgfgf";
      str=str.replace('f',' ');
      System.out.println(str);
      String v="f";
      try{
      str="abcdefgfgf";
      str=str.replaceAll(v,str);
      System.out.println(str);
    }catch(java.util.regex.PatternSyntaxException e){
    e.printStackTrace();
    }
      }
    }raplaceAll怎么用,看不明白,谢谢有人能指导吗???
      

  7.   

    str=str.replaceAll(v,str);
    晕,看明白了,以v代替str; 如 替换空格
    mystring.replaceAll("[\r\n]","<br>");
      

  8.   

    我要用的是1.3
    不能用replaceAll
    还有我用replace是把一个字符替换为空字符,不是空格!!!
      

  9.   

    char str='';
    String.replace('f',str);
    试一下可不可以
      

  10.   

    空子符好像就是没有的意思。就是不存在的意思。null和空还不太一样。不信可以测试一下
    if(null=="")
    你可以测试一下"a"+""+"b";的结果。好像没有必要这样吧?如果真的需要给特出的东西留下空间可以用自定一个。str=str.replaceAll(v,"");看看能不能用。我的机器没有这个函数。实在不行就自己写一个吧,反正也好写。