Java replaceFirst () วิธีการ
replaceFirst () วิธีการใช้พารามิเตอร์ที่กำหนดของสตริงทดแทนการเปลี่ยนครั้งแรกตรงกับที่กำหนดย่อยแสดงออกปกติ
ไวยากรณ์
public String replaceFirst(String regex, String replacement)
พารามิเตอร์
regex - ตรงกับสตริงนิพจน์ปกติ
ทดแทน - เพื่อแทนที่เกิดขึ้นครั้งแรกของสตริง
ราคาย้อนกลับ
ความสำเร็จของสตริงทดแทนจะถูกส่งกลับแล้วล้มเหลวในการส่งกลับสายเดิม
ตัวอย่าง
public class Test { public static void main(String args[]) { String Str = new String("hello w3big,I am from w3big。"); System.out.print("返回值 :" ); System.out.println(Str.replaceFirst("w3big", "google" )); System.out.print("返回值 :" ); System.out.println(Str.replaceFirst("(.*)w3big(.*)", "google" )); } }
ดังกล่าวข้างต้นผลการทำงานของโปรแกรม:
匹配成功返回值 :w3big 匹配失败返回值 :www.google.com