标签:length nbsp 返回 开始 stringbu sub buffer substring public
public StringBuffer replace(int start,int end, String str);
从start开始到end用str替换
1 public static void main(String[] args) { 2 StringBuffer sb1 = new StringBuffer("baoma"); 3 sb1.replace(3, 5, "bei"); 4 System.out.println(sb1); 5 6 }
public StringBuffer reverse();
字符串反转
1 public static void main(String[] args) { 2 StringBuffer sb1 = new StringBuffer("baoma"); 3 sb1.reverse(); 4 System.out.println(sb1); 5 }
public String substring(int start)
从指定位置截取到末尾
public String substring(int start,int end)
截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
注意:返回值类型不再是StringBuffer本身
1 public static void main(String[] args) { 2 StringBuffer sb1 = new StringBuffer("baiheia"); 3 String str = sb1.substring(5); 4 System.out.println(str); 5 System.out.println(sb1); 6 String str2= sb1.substring(0,5); 7 System.out.println(str2); 8 }
1,通过构造方法
2,通过append()方法
1,通过构造方法
2,通过toString()方法
3,通过subString(0,length);
1 public static void main(String[] args) { 2 //demo1(); String 转 StringBuffer 3 StringBuffer sb1=new StringBuffer("woaiheima"); 4 //StringBuffer转String 5 String s1=new String(sb1); 6 System.out.println(s1); 7 8 //通过toString方法将StringBuffer转String 9 System.out.println(s1.toString()); 10 11 //通过截取字符串将StringBuffer转换为String 12 String s2=sb1.substring(0,sb1.length()); 13 System.out.println(s2); 14 15 16 } 17 18 private static void demo1() { 19 //通过构造方法将字符串转换为StringBuffer对象 20 StringBuffer sb1=new StringBuffer("woaiheima"); 21 System.out.println(sb1); 22 23 //通过append方法将字符串转化为StringBuffer对象 24 StringBuffer sb2=new StringBuffer(); 25 sb2.append("heima"); 26 System.out.println(sb2); 27 28 }
标签:length nbsp 返回 开始 stringbu sub buffer substring public
原文地址:https://www.cnblogs.com/Sherwin-liao/p/10986781.html