Java String replace()方法返回一個使用newChar
替換此字串中所有出現的oldChar
的字串。
語法
以下是此方法的語法 -
public String replace(char oldChar, char newChar)
參數
oldChar
- 老字元。newChar
- 新字元。
返回值
- 它返回一個使用
newChar
替換newChar
後的新字串。
示例
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to xuhuhu.com");
System.out.print("Return Value :" );
System.out.println(Str.replace('W', 'T'));
System.out.print("Return Value :" );
System.out.println(Str.replace('i', 'L'));
}
}
執行上面查詢語句,得到以下結果:
Return Value :Telcome to xuhuhu.com
Return Value :Welcome to YLLbaL.com
上一篇:
Java String類
下一篇:
Java快速入門