用另一种字符替换特定字符的 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