学JAVA的新新手 昨天做了个题 替换字符串的 但 运行起来却看不到输出  帮忙看看 谢谢~~
public class Asd{
public Asd(){}
public String repast(String Sou,String oldString,String newString ){
StringBuffer bf = new StringBuffer();int Slen = Sou.length();
int Olen= oldString.length();
int s=0;
int pos;
while((s=Sou.indexOf(oldString))>=0){
bf.append(Sou.substring(0,s)).append(newString);
pos= s+Olen;
if (pos < Slen) { 
bf.append(Sou.substring(pos)); 

}
return bf.toString(); }
public static void main(String args[]){
String s="SOURCE";
String oldString="RC";
String newString="QW";
String i;
Asd a= new Asd();
i= a.repast(s,oldString,newString);
System.out.println(""+i);}
}

解决方案 »

  1.   

    public class Asd{
    public Asd(){}
    public String repast(String Sou,String oldString,String newString ){
    StringBuffer bf = new StringBuffer();int Slen = Sou.length();
    int Olen= oldString.length();
    int s=0;
    int pos;
    while((s=Sou.indexOf(oldString))>=0){
    bf.append(Sou.substring(0,s)).append(newString);
    pos= s+Olen;
    if (pos < Slen) { 
    bf.append(Sou.substring(pos)); 
    }
    Sou = Sou.substring(pos);//少了这句,你的while循环一直成立,因为你的Sou一直没变
    }
    return bf.toString(); }
    public static void main(String args[]){
    String s="SOURCE";
    String oldString="RC";
    String newString="QW";
    String i;
    Asd a= new Asd();
    i= a.repast(s,oldString,newString);
    System.out.println(""+i);}
    }