replace之后,str的值保持不变
你可以str=str.replace('h','p');

解决方案 »

  1.   

    public String String.replace()replace() will return the result as a new String
    String s=str.replace('h','p');String is a final class, the content cannot be changed.
      

  2.   

    楼主用的方法只能将hello改成pello
    要改成phllo,就用str.replaceAll("he","ph");String与对象不同,不是修改以后就失去了对原来的引用
    要写成str = str.replace('h','p');
      public static void main(String args[]) {
        String str = new String("hello");
        str = str.replace('h', 'p');
        System.out.println(str);
      }
      

  3.   

    public String String.replace()replace() will return the result as a new String
    String s=str.replace('h','p');String is a final class, the content cannot be changed.
    ------------------------------------
    GOOD
      

  4.   

    String 是不变的
    你这样看看
        str = str.replace('h', 'p');
      

  5.   

    也就是java,换了c++,早就内存泄露了。我开始越来越喜欢java了。
      

  6.   

    请看一看Java API,其中说的非常清楚:
    Strings are constant; their values cannot be changed after they are created.
    String 是常数, 恒量,String的值是不能修改的。    String str=new String("hello");
        str.replace('h','p');
        System.out.println(str); //输出hello
        System.out.println(str.replace('h','p'); //会输出pello
      

  7.   

    看String的文档说明Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. 
    If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar. 替换后新串被方法返回,但并没有改变原串,仔细阅读他