Java แทนที่ () วิธีการ
แทนที่ () วิธีการแทนที่ตัวอักษร oldChar ด้วยตัวอักษร newChar และส่งกลับสตริงใหม่ทดแทน
ไวยากรณ์
public String replace(char oldChar, char newChar)
พารามิเตอร์
oldChar - ตัวละครเดิม
newChar - ตัวละครใหม่
ราคาย้อนกลับ
แทนที่ด้วยสตริงใหม่แล้วรุ่นเล่า
ตัวอย่าง
public class Test { public static void main(String args[]) { String Str = new String("hello"); System.out.print("返回值 :" ); System.out.println(Str.replace('o', 'T')); System.out.print("返回值 :" ); System.out.println(Str.replace('l', 'D')); } }
ดังกล่าวข้างต้นผลการทำงานของโปรแกรม:
返回值 :hellT 返回值 :heDDo