Java replaceAll() 方法
replaceAll() 方法使用給定的參數 replacement 替換字串所有匹配給定的正則運算式的子字串。
語法
public String replaceAll(String regex, String replacement)
參數
regex -- 匹配此字串的正則運算式。
newChar -- 用來替換每個匹配項的字串。
返回值
成功則返回替換的字串,失敗則返回原始字串。
實例
public class Test {
public static void main(String args[]) {
String Str = new String("www.google.com");
System.out.print("匹配成功返回值 :" );
System.out.println(Str.replaceAll("(.*)google(.*)", "zaixian" ));
System.out.print("匹配失敗返回值 :" );
System.out.println(Str.replaceAll("(.*)taobao(.*)", "zaixian" ));
}
}
以上程式執行結果為:
匹配成功返回值 :zaixian 匹配失敗返回值 :www.google.com

Java String類