Java 實(shí)例 - 字符串替換

Java 實(shí)例 Java 實(shí)例

如何使用java替換字符串中的字符呢?

以下實(shí)例中我們使用 java String 類的 replace 方法來(lái)替換字符串中的字符:

public class StringReplaceEmp{
   public static void main(String args[]){
      String str="Hello World";
      System.out.println( str.replace( 'H','W' ) );
      System.out.println( str.replaceFirst("He", "Wa") );
      System.out.println( str.replaceAll("He", "Ha") );
   }
}

以上代碼實(shí)例輸出結(jié)果為:

Wello World
Wallo World
Hallo World

Java 實(shí)例 Java 實(shí)例