如何在 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
java_strings.html

