java replace()函數的用法

replace() 方法通過用 newChar 字符替換字符串中出現的所有 oldChar 字符,並返回替換後的新字符串。

語法:

public String replace(char oldChar,  char newChar)

參數解釋:

oldChar -- 原字符;

newChar -- 新字符。

實例:

public class Test {
    public static void main(String args[]) {
        String Str = new String("hello");

        System.out.print("結果 :" );
        System.out.println(Str.replace('h', 'c'));
}
}

運行結果爲:

返回值 :cello


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