Java - String replaceAll() 方法



Java String replaceAll() 方法用於替換 String 物件中所有特定模式的出現。

此方法接受正則表示式和替換字串作為引數,搜尋當前字串以查詢給定的正則表示式,並將匹配的子字串替換為給定的替換字串。

replaceAll() 方法和 replace() 方法之間的唯一區別在於 replaceAll() 方法替換模式(使用正則表示式)。而 replace() 方法替換字串。

注意:請注意,如果替換字串包含美元符號 ($) 或反斜槓 (\),則結果可能與將其作為文字替換字串處理的結果不同。

語法

以下是Java String replaceAll()方法的語法:

public String replaceAll(String regex, String replacement)

引數

  • regex - 這是要與此字串匹配的正則表示式。

  • replacement - 這是要替換每個匹配項的字串。

返回值

此方法返回結果字串。

從字串示例中替換元字元

以下示例顯示了透過替換正則表示式中給出的元字元來使用 Java String replaceAll() 方法:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str1 = "!!Tutorials!!Point", str2;
      String substr = "**", regex = "!!";    
      
      // prints string1
      System.out.println("String = " + str1);    
      
      /* replaces each substring of this string that matches the given
      regular expression with the given replacement */
      str2 = str1.replaceAll(regex, substr);    
      System.out.println("After Replacing = " + str2);
   }
}

輸出

如果編譯並執行以上程式,它將產生以下結果:

String = !!Tutorials!!Point
After Replacing = **Tutorials**Point

從字串示例中替換單詞的出現

在以下示例中,使用 replaceAll() 方法替換單詞的所有出現:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String str = "Next time there won't be a next time after a certain time.";
      System.out.println("The given string is: " + str);
      
      // replacing all occurrences of "time" to "day"
      String newString = str.replaceAll("time", "day");
      System.out.println("The new replaced string is: " + newString);
   }
}

輸出

如果編譯並執行以上程式,輸出將顯示如下:

The given string is: Next time there won't be a next time after a certain time.
The new replaced string is: Next day there won't be a next day after a certain day.

從字串示例中替換所有空格字元

現在,我們將使用 replaceAll() 方法刪除給定字串中所有空格的出現:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String str = "Next time there won't be a next time after a certain time.";
      System.out.println("The given string is: " + str);
      
      // replacing all occurrences of "time" to "day"
      String newString = str.replaceAll("\\s", "");
      System.out.println("The new replaced string is: " + newString);
   }
}

輸出

執行以上程式後,輸出如下:

The given string is: Next time there won't be a next time after a certain time.
The new replaced string is: Nexttimetherewon'tbeanexttimeafteracertaintime.

檢查無效模式示例的 PatternSyntaxException

無效的正則表示式會導致 replaceAll() 方法丟擲 PatternSyntaxException。請檢視以下示例:

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "Welcome to tutorials point.";
      
      // invalid regular expression provided.
      String regExpression = "\\";
      s = s.replaceAll(regExpression, "point ");
      System.out.println("The replaced string is: " + s); 
   }
}

PatternSyntaxException

以上程式的輸出如下:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
      at java.base/java.util.regex.Pattern.error(Pattern.java:2038)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1799)
      at java.base/java.util.regex.Pattern.(Pattern.java:1440)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1079)
      at java.base/java.lang.String.replaceAll(String.java:2938)
      at StringDemo.main(StringDemo.java:7)
java_lang_string.htm
廣告