用另一種字元替換特定字元的 Java 程式設計
使用 replace() 方法用另一個字元替換一個特定的字元。假設以下內容是我們的字串,我們在這裡將空格替換為 $ 字元。
String str1 = "Orange is the new Black!";
現在,使用 replace() 方法用 $ 替換字元
str1.replace(' ', '$');示例
public class Demo {
public static void main(String[] args) {
String str1 = "Orange is the new Black!";
System.out.println("String: "+str1);
String str2 = str1.replace(' ', '$');
System.out.println("Updated string: "+str2);
}
}輸出
String: Orange is the new Black! Updated string: Orange$is$the$new$Black!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP