标签:
1、StringBuffer的替换功能:
public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
1 public class StringBufferDemo { 2 public static void main(String[] args) { 3 // 创建字符串缓冲区对象 4 StringBuffer sb = new StringBuffer(); 5 6 // 添加数据 7 sb.append("hello"); 8 sb.append("world"); 9 sb.append("java"); 10 System.out.println("sb:" + sb); 11 12 // public StringBuffer replace(int start,int end,String 13 // str):从start开始到end用str替换 14 // 需求:我要把world这个数据替换为"节日快乐" 15 sb.replace(5, 10, "节日快乐"); 16 System.out.println("sb:" + sb); 17 } 18 }
2、StringBuffer的反转功能:
public StringBuffer reverse()
1 public class StringBufferDemo { 2 public static void main(String[] args) { 3 // 创建字符串缓冲区对象 4 StringBuffer sb = new StringBuffer(); 5 6 // 添加数据 7 sb.append("霞青林爱我"); 8 System.out.println("sb:" + sb); 9 10 // public StringBuffer reverse() 11 sb.reverse(); 12 System.out.println("sb:" + sb); 13 } 14 }
3、StringBuffer的截取功能:注意返回值类型不再是StringBuffer本身了,而是String
public String substring(int start)
public String substring(int start,int end)
1 public class StringBufferDemo { 2 public static void main(String[] args) { 3 // 创建字符串缓冲区对象 4 StringBuffer sb = new StringBuffer(); 5 6 // 添加元素 7 sb.append("hello").append("world").append("java"); 8 System.out.println("sb:" + sb); 9 10 // 截取功能 11 // public String substring(int start) 12 String s = sb.substring(5); 13 System.out.println("s:" + s); //worldjava 14 System.out.println("sb:" + sb); //helloworldjava,因为上面返回的是String,不是本身,所以不改变 15 16 // public String substring(int start,int end) 17 String ss = sb.substring(5, 10); 18 System.out.println("ss:" + ss); // world 19 System.out.println("sb:" + sb); // helloworldjava 20 } 21 }
java 12-4 StringBuffer类的替换、反转、截取功能
标签:
原文地址:http://www.cnblogs.com/LZL-student/p/5877371.html