當使用方法裏面的String參數的時候,在方法當中用replace操作字符串是不能操作的,因爲String是引用類型,replace可以直接操作字符串可以參考replace方法源碼,我是用javabean傳的,表示有用!
public class SanShu { private String string; private String yl; private String th; public SanShu(){ } public SanShu(String yl, String th,String string) { this.yl = yl; this.th = th; this.string = string; } public String getYl() { return yl; } public void setYl(String yl) { this.yl = yl; } public String getTh() { return th; } public void setTh(String th) { this.th = th; } public String getString() { return string; } public void setString(String string) { this.string = string; } }
這是一個簡單的javabean
public class TestMethed { public String replaceSt(SanShu sanShu){ System.out.println(sanShu.getString()); String replace = sanShu.getString().replaceAll(sanShu.getYl(),sanShu.getTh()); System.out.println(replace); return replace; } }
這是方法類。
public class YanMing { public static void main(String [] args){ //String string = "654321"; TestMethed testMethed = new TestMethed(); // testMethed.run(string); String s = testMethed.replaceSt(new SanShu("54","3","654321")); System.out.println(s); } }
這是主方法。