1. StringBuffer的替换功能:
public StringBuffer replace(int start, int end, String str):
2. 案例演示:
1 package cn.itcast_04; 2 3 /* 4 * StringBuffer的替换功能: 5 * public StringBuffer replace(int start,int end,String str):从start开始到end用str替换 6 */ 7 public class StringBufferDemo { 8 public static void main(String[] args) { 9 // 创建字符串缓冲区对象 10 StringBuffer sb = new StringBuffer(); 11 12 // 添加数据 13 sb.append("hello"); 14 sb.append("world"); 15 sb.append("java"); 16 System.out.println("sb:" + sb); 17 18 // public StringBuffer replace(int start,int end,String 19 // str):从start开始到end用str替换 20 // 需求:我要把world这个数据替换为"节日快乐" 21 sb.replace(5, 10, "节日快乐"); //索引包含5,6,7,8,9(包含左边5,不包含右边10) 22 System.out.println("sb:" + sb); 23 } 24 }
运行结果: