Java 教程 Java 实例 - 字符串替换

kyle · 2022-06-10 23:42:10 · 热度: 13

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

以下实例中我们使用 java String 类的 replace 方法来替换字符串中的字符:

StringReplaceEmp.java 文件

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") ); } }

以上代码实例输出结果为:

Wello World
Wallo World
Hallo World

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册