Java 教程
replaceFirst() 方法使用給定的參數(shù) replacement 替換字符串第一個(gè)匹配給定的正則表達(dá)式的子字符串。
public String replaceFirst(String regex, String replacement)
regex -- 匹配此字符串的正則表達(dá)式。
replacement -- 用來替換第一個(gè)匹配項(xiàng)的字符串。
成功則返回替換的字符串,失敗則返回原始字符串。
public class Test { ????public static void main(String args[]) { ????????String Str = new String("hello json,I am from json。"); ????????System.out.print("返回值 :" ); ????????System.out.println(Str.replaceFirst("json", "google" )); ????????System.out.print("返回值 :" ); ????????System.out.println(Str.replaceFirst("(.*)json(.*)", "google" )); ????} }
以上程序執(zhí)行結(jié)果為:
返回值 :hello google,I am from json。 返回值 :google