java關於替換字符串傳參問題

        當使用方法裏面的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);
    }
}

這是主方法。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章