Java - String replace() 方法



Java String replace() 方法用於替換字串物件中單個字元或子字串為給定值。

此方法在當前字串中搜索給定的字元(或子字串),將所有出現的字元替換為給定的值,並返回結果字串。此替換從字串的開頭到結尾執行。替換意味著提供合適的替代方案、等價物或將其恢復到原始狀態。

此方法有兩個多型變體。下面顯示了這兩個變體的語法。此方法的字元變體將字串中舊字元的每個例項替換為新字元。類似地,字元序列變體替換給定的子字串。

例如,在字串“ccc”中將“cc”替換為“b”將得到“bc”而不是“cb”。

語法

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

public String replace(CharSequence target, CharSequence replacement)
or,
public String replace(char oldChar, char newChar) 

引數

  • target - 要替換的 char 值序列。 // 第一個語法

  • replacement - 替換的 char 值序列。 // 第一個語法

  • oldChar - 舊字元。 // 第二個語法

  • newChar - 新字元。 // 第二個語法

返回值

  • public String replace(CharSequence target, CharSequence replacement) 方法返回結果字串。 // 第一個語法

  • public String replace(char oldChar, char newChar) 方法返回一個從該字串派生的字串,方法是用 newChar 替換 oldChar 的每次出現。 // 第二個語法

使用 CharSequence 在 String 中替換 CharSequence 的示例

在下面給出的示例中,初始化了一個 String 物件“str”。然後,初始化兩個 charSequence,其中上面建立的字串中的第一個 charSequence 被第二個 charSequence 替換:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "Tutorialspoint";
      System.out.println("string = " + str); 
      CharSequence s1 = "rialspoint";
      CharSequence s2 = "rix";      
      
      // replace sequence s1 with s2
      String replaceStr = str.replace(s1, s2); 
      
      // prints the string after replacement
      System.out.println("new string = " + replaceStr);
   }
}

輸出

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

string = Tutorialspoint
new string = Tutorix

使用 CharSequence 替換 CharSequence 的示例

以下示例演示了 Java String replace() 方法的使用,透過該方法,將舊字元序列“how”的所有出現替換為新字元序列“why”:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "Hello how are how you";
      System.out.println("string = " + str); 
      CharSequence s1 = "how";
      CharSequence s2 = "why";      
      
      // replace sequence s1 with s2
      String replaceStr = str.replace(s1, s2); 
      
      // prints the string after replacement
      System.out.println("new string = " + replaceStr);
   }
}

輸出

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

string = Hello how are how you
new string = Hello why are why you

使用 String 替換 String 的示例

在下面給出的程式碼中,使用 replace() 方法將字元 't' 的所有出現替換為新字元 'f':

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = new String("Welcome to tutorials point training");
      System.out.println("The given string is: " + str);
      String newString = new String();
      newString = str.replace('t', 'f');
      System.out.println("The new string now is: " + newString);
   }
}

輸出

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

The given string is: Welcome to tutorials point training
The new string now is: Welcome fo fuforials poinf fraining

使用 String 替換 String 的示例

在下面給出的程式碼中,使用 replace() 方法將字串 'time' 的所有出現替換為新字串 'day':

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = new String("Next time there won't be a next time.");
      System.out.println("The string is: " + str);
      
      // Replacing the old string with new string
      String newString = new String();
      newString = str.replace("time", "day");
      System.out.println("The replaced string is: " + newString);
   }
}

輸出

上述程式的輸出如下:

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

檢查替換空字串時的異常示例

當替換或目標為 null 時,replace() 方法將丟擲 NullPointerException。以下示例支援這一點:

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "Next time there won't be a next time.";
      int size = s.length();
      System.out.println("The string is: " + s);
      String newString = null;
      s = s.replace(newString, "time ");
      System.out.println("The replaced string is: " + s);
   }
}

NullPointerException

執行上述程式碼時,我們得到以下輸出

The string is: Next time there won't be a next time.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "target" is null
      at java.base/java.lang.String.replace(String.java:2954)
      at StringDemo.main(StringDemo.java:7)
java_lang_string.htm
廣告