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