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('o', 'T')); System.out.print("返回值 :" ); System.out.println(Str.replace('l', 'D')); } }
以上程式執行結果為:
返回值 :hellT 返回值 :heDDo