當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.lang.string.replace()用法及代碼示例


這是replace()方法的三個變體。本文介紹了它們的全部內容,如下所示:1.字符串replace():此方法返回一個新字符串,該字符串是通過用新字符替換字符串中所有舊字符而得到的。

用法:
public String replace(char oldch, char newch)
參數:
oldch:the old character.
newch :the new character.
Return Value
It returns a string derived from this string by replacing every occurrence of oldch with newch.
// Java code to demonstrate the 
// working of  replace()  
public class rep1 { 
   public static void main(String args[]) { 
         
      // Initialising String 
      String Str = new String("Welcome to geeksforgeeks"); 
        
      // Using replace to replace characters 
      System.out.print("After replacing all o with T:" ); 
      System.out.println(Str.replace('o', 'T')); 
        
      // Using replace to replace characters 
      System.out.print("After replacing all e with D:" ); 
      System.out.println(Str.replace('e', 'D')); 
        
   } 
}

輸出:

After replacing all o with T:WelcTme tT geeksfTrgeeks
After replacing all e with D:WDlcomD to gDDksforgDDks

2.字符串replaceAll():此方法用給定的replace_str替換與給定的正則表達式匹配的字符串的每個子字符串。


用法:
public String replaceAll(String regex, String replace_str)
參數:
regex:the regular expression to which this string is to be matched.
replace_str:the string which would replace found expression.
Return Value
This method returns the resulting String.
// Java code to demonstrate the 
// working of  replaceAll()  
public class rep2 { 
   public static void main(String args[]) { 
         
      // Initialising String 
      String Str = new String("Welcome to geeksforgeeks"); 
        
      // original string  
      System.out.print("Original String:" ); 
      System.out.println(Str); 
        
      // Using replaceAll to replace regex with replace_str 
      System.out.print("After replacing regex with replace_str:" ); 
      System.out.println(Str.replaceAll("(.*)geeks(.*)", "ASTHA TYAGI")); 
        
   } 
}

輸出:

Original String:Welcome to geeksforgeeks
After replacing regex with replace_str:ASTHA TYAGI

3.字符串replaceFirst():此方法用給定的replace_str替換與給定的正則表達式匹配的該字符串的第一個子字符串。

Syntax
public String replaceFirst(String regex, String replace_str)
參數
regex:the regular expression to which this string is to be matched.
replace_str: the string which would replace found expression.
返回值:
This method returns a resulting String.
// Java code to demonstrate the 
// working of  replaceFirst()  
public class rep3 { 
   public static void main(String args[]) { 
         
      // Initialising String 
      String Str = new String("Welcome to geeksforgeeks"); 
        
      // original string  
      System.out.print("Original String:" ); 
      System.out.println(Str); 
        
      // Using replaceFirst to replace regex with replace_str 
      // Replaces 1st occurrence of geeks with ASTHA 
      System.out.print("After replacing 1st occurrence of regex with replace_str :" ); 
      System.out.println(Str.replaceFirst("geeks", "ASTHA")); 
        
   } 
}

輸出:

Original String:Welcome to geeksforgeeks
After replacing 1st occurrence of regex with replace_str :Welcome to ASTHAforgeeks


相關用法


注:本文由純淨天空篩選整理自 Java.lang.string.replace() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。