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

更新时间:2020 年 6 月 25 日

529 次观看

启动您的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.