Java replaceAll () วิธีการ
replaceAll () วิธีการใช้การเปลี่ยนค่าพารามิเตอร์ที่กำหนดของทุกสตริงทดแทนตรงกับการแสดงออกปกติรับ substring
ไวยากรณ์
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