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