package stringTest; public class StringDemo2 { public static void main(String[] args) { String s = "acvbb"; String s1 = new String("acvbb"); String s2 = s.replace("a", "X"); String s3 = s1.replace("a", "X"); /** * String类中的replace方法表示字符串替换,不改变原始的字符串; * 该方法返回替换后的字符串 * */ System.out.println("s = " + s); //s = acvbb System.out.println("s2 = " + s2); //s2 = Xcvbb System.out.println("s1 = " + s1); //s1 = acvbb System.out.println("s3 = " + s3); //s3 = Xcvbb } }
打印输出结果:
s = acvbb s2 = Xcvbb s1 = acvbb s3 = Xcvbb
时间: 2024-10-14 14:29:16