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(.*)", "w3big" )); System.out.print("匹配失败返回值 :" ); System.out.println(Str.replaceAll("(.*)taobao(.*)", "w3big" )); } }
以上程序執行結果為:
匹配成功返回值 :w3big 匹配失败返回值 :www.google.com