package com.shujia.day11;
/*
StringBuffer的功能:
添加功能
public StringBuffer append(String str) 在末尾处添加字符,返回自身
public StringBuffer insert(int offset,String str) 指定位置添加字符串,返回自身
删除功能
public StringBuffer deleteCharAt(int index) 根据索引删除某一个字符
public StringBuffer delete(int start,int end) 根据索引指定删除某一个字符串[start, end)
替换功能
public StringBuffer replace(int start,int end,String str) 使用一个新的字符串将一段字符串进行替换 [start, end)
反转功能
public StringBuffer reverse() 将字符串反转(字符串逆序)
截取功能
public String substring(int start)
public String substring(int start,int end)
*/
public class StringBufferDemo2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println("sb: " + sb);
System.out.println("-------------------------------------");
// public StringBuffer append(String str) 在末尾处添加字符,返回自身
// 任何数据类型值一旦进入到StringBuffer中就是一个一个的普通字符
sb.append(100);
sb.append(12.345);
sb.append(true);
sb.append(1000L);
sb.append("hello");
System.out.println("sb: " + sb);
System.out.println("-------------------------------------");
//public StringBuffer insert(int offset,String str) 指定位置添加字符串,返回自身
sb.insert(9, "_____");
System.out.println("sb: " + sb);
System.out.println("-------------------------------------");
//10012.345_____true1000hello
//public StringBuffer deleteCharAt(int index) 根据索引删除某一个字符
// sb.deleteCharAt(14);
// System.out.println("sb: " + sb);
System.out.println("-------------------------------------");
//public StringBuffer delete(int start,int end) [start, end)
// sb.delete(9,14);
// System.out.println("sb: " + sb);
System.out.println("-------------------------------------");
//public StringBuffer replace(int start,int end,String str) 使用一个新的字符串将一段字符串进行替换 [start, end)
sb.replace(9, 14, "aaaaaa");
System.out.println("sb: " + sb);
System.out.println("-------------------------------------");
//public StringBuffer reverse()
StringBuffer sb2 = new StringBuffer("hello");
sb2.reverse();
System.out.println("sb2: " + sb2);
/*
sb2: olleh
*/
截取功能:
StringBuffer sb = new StringBuffer("helloworld");
System.out.println("sb: " + sb);
System.out.println("---------------------------------");
String res1 = sb.substring(4);
System.out.println("sb: " + sb);
System.out.println("res1: " + res1);
System.out.println("---------------------------------");
//public String substring(int start,int end) 截取一部分
String res2 = sb.substring(4, 7);
System.out.println("sb: " + sb);
System.out.println("res2: " + res2);
}
}