Java 示例 - 替换字符串 问题描述 如何用另一个替换字符串中的子字符串? 解决方案 这个例子描述了如何使用java String 类的replace 方法将字符或子串替换为新的。 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 复制