Java 教程
replaceAll() 方法使用給定的參數(shù) 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(.*)", "json" )); ????????System.out.print("匹配失敗返回值 :" ); ????????System.out.println(Str.replaceAll("(.*)taobao(.*)", "json" )); ????} }
以上程序執(zhí)行結(jié)果為:
匹配成功返回值 :json 匹配失敗返回值 :www.google.com