该文章Github地址:https://github.com/AntonyCheng/java-notes
在此介绍一下作者开源的SpringBoot项目初始化模板(Github仓库地址:https://github.com/AntonyCheng/spring-boot-init-template & CSDN文章地址:https://blog.csdn.net/AntonyCheng/article/details/136555245),该模板集成了最常见的开发组件,同时基于修改配置文件实现组件的装载,除了这些,模板中还有非常丰富的整合示例,同时单体架构也非常适合SpringBoot框架入门,如果觉得有意义或者有帮助,欢迎Star & Issues & PR!
上一章:由浅到深认识Java语言(18):权限修饰符&包&Object类
31.String类
定义:
Java 中所有的字符串的字面值,都是 String 类的对象;
由来:
String 底层是一个字符数组,字符数组里放着多个字符,每一个字符通过编码表在底层用二进制存储;
编码表(字符集)
任何数据,包括字符,在内存里存的就是二进制;
字符是通过编码表转换成二进制的,也就是说在编码表中,能够找到每一个字符对应的二进制;
常见的编码表有:
ASCII 码表(使用一个字节来描述一个字符),unicode 编码表,UTF-8(使用三字节来描述一个字符),GBK(使用两个字节来描述一个字符),GBK2312;
ASCII码表需要记住 0 ==> 48,A ==> 65,a ==> 97
其中 unicode 和 GBK 能够描述中文;
结论:
字符串是用字符数组来存储字符的,字符是用二进制来存储的,那么字符和二进制之间的对应关系是通过编码表来解决的,不同的编码表,字符对应的二进制是不同的;
编码和解码:
编码:将字符通过编码表(字符集)转换成二进制的过程,称为编码,也可以简单的认为把看得懂的字符变成看不懂的二进制;
解码:将二进通过编码表(字符集)制转换成字符的过程,称为解码,也可以简单的认为把看不懂的二进制变成看得懂的字符;
String构造器
| 构造方法摘要 | 
|---|
| String()初始化一个新创建的String对象,使其表示一个空字符序列。 | 
| String(byte[] bytes)通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的String,获得一个 byte 数组:str.getBytes()。 | 
| String(byte[] bytes, Charset charset)通过使用指定的 charset 解码指定的 byte 数组,构造一个新的String,指定编码表示例:Charset.forName(“utf-8”) 或者直接 “utf-8”。 | 
| String(byte[] ascii, int hibyte)已过时。 该方法无法将字节正确地转换为字符。从 JDK 1.1 开始,完成该转换的首选方法是使用带有Charset、字符集名称,或使用平台默认字符集的String构造方法。 | 
| String(byte[] bytes, int offset, int length)通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的String,从第 offset 位截取字节长度为 length。 | 
| String(byte[] bytes, int offset, int length, Charset charset)通过使用指定的 charset 解码指定的 byte 子数组,构造一个新的String。 | 
| String(byte[] ascii, int hibyte, int offset, int count)已过时。 该方法无法将字节正确地转换为字符。从 JDK 1.1 开始,完成该转换的首选方法是使用带有Charset、字符集名称,或使用平台默认字符集的String构造方法。 | 
| String(byte[] bytes, int offset, int length, String charsetName)通过使用指定的字符集解码指定的 byte 子数组,构造一个新的String。 | 
| String(byte[] bytes, String charsetName)通过使用指定的 charset 解码指定的 byte 数组,构造一个新的String。 | 
| String(char[] value)分配一个新的String,使其表示字符数组参数中当前包含的字符序列。 | 
| String(char[] value, int offset, int count)分配一个新的String,它包含取自字符数组参数一个子数组的字符,从第 offset 位开始截取 count 个数。 | 
| String(int[] codePoints, int offset, int count)分配一个新的String,它包含 Unicode 代码点数组参数一个子数组的字符。 | 
| String(String original)初始化一个新创建的String对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。 | 
| String(StringBuffer buffer)分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列。 | 
| String(StringBuilder builder)分配一个新的字符串,它包含字符串生成器参数中当前包含的字符序列。 | 
常用构造方法
String():
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String();}
}
String(byte[] bytes):
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {byte[] bytes = {'a','b','c','d','e'};String str = new String(bytes);System.out.println(str);}
}
打印效果如下:

String(byte[] bytes, Charset charset):
package top.sharehome.Bag;
import java.io.UnsupportedEncodingException;
public class Demo {public static void main(String[] args) throws UnsupportedEncodingException {byte[] bytes = {'a','b','c','d','e'};String str = new String(bytes,"utf-8");System.out.println(str);}
}
打印效果如下:

String(byte[] bytes, int offset, int length):
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {byte[] bytes = {'a','b','c','d','e'};String str = new String(bytes,3,1);System.out.println(str);}
}
打印效果如下:

String(char[] value, int offset, int count):
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {char[] c = {'a','b','c','d','e'};String str = new String(c,3,1);System.out.println(str);}
}
打印效果如下:

String方法
| 返回类型 | 方法解释 | 
|---|---|
| char | charAt(int index)返回指定索引处的char值。 | 
|  int | codePointAt(int index)返回指定索引处的字符(Unicode 代码点)。 | 
|  int | codePointBefore(int index)返回指定索引之前的字符(Unicode 代码点)。 | 
|  int | codePointCount(int beginIndex, int endIndex)返回此String的指定文本范围中的 Unicode 代码点数。 | 
|  int | compareTo(String anotherString)按字典顺序比较两个字符串,返回值是两字符的字符数之间的差值(大小写需相同)。 | 
|  int | compareToIgnoreCase(String str)按字典顺序比较两个字符串,不考虑大小写,返回值是两字符的字符数之间的差值。 | 
|  String | concat(String str)将指定字符串连接到此字符串的结尾。 | 
|  boolean | contains(CharSequence s)当且仅当此字符串包含指定的 char 值序列时,返回 true,以接口方式存在,以多态方式运用。 | 
|  boolean | contentEquals(CharSequence cs)将此字符串与指定的CharSequence比较。 | 
|  boolean | contentEquals(StringBuffer sb)将此字符串与指定的StringBuffer比较。 | 
| static String | copyValueOf(char[] data)返回指定数组中表示该字符序列的 String。 | 
| static String | copyValueOf(char[] data, int offset, int count)返回指定数组中表示该字符序列的 String。 | 
|  boolean | endsWith(String suffix)测试此字符串是否以指定的后缀结束。 | 
|  boolean | equals(Object anObject)将此字符串与指定的对象比较。 | 
|  boolean | equalsIgnoreCase(String anotherString)将此String与另一个String比较,不考虑大小写。 | 
| static String | format(Locale l, String format, Object... args)使用指定的语言环境、格式字符串和参数返回一个格式化字符串。 | 
| static String | format(String format, Object... args)使用指定的格式字符串和参数返回一个格式化字符串。 | 
|  byte[] | getBytes()使用平台的默认字符集将此String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 | 
|  byte[] | getBytes(Charset charset)使用给定的 charset 将此String编码到 byte 序列,并将结果存储到新的 byte 数组。 | 
|  void | getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过getBytes()方法,该方法使用平台的默认字符集。 | 
|  byte[] | getBytes(String charsetName)使用指定的字符集将此String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 | 
|  void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)将字符从此字符串复制到目标字符数组,参数为(源字符串复制起始位,源字符串字符个数,目标字符串,目标字符串开始复制位置)。 | 
|  int | hashCode()返回此字符串的哈希码。 | 
|  int | indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引,此时的参数 int 也运用了多态,不仅可以传 int,还可以是 char,若没找到返回 -1。 | 
|  int | indexOf(int ch, int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 | 
|  int | indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引。 | 
|  int | indexOf(String str, int fromIndex)返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 | 
|  String | intern()返回字符串对象的规范化表示形式。 | 
|  boolean | isEmpty()当且仅当length()为0时返回true。 | 
|  int | lastIndexOf(int ch)返回指定字符在此字符串中最后一次出现处的索引,此时的参数 int 也运用了多态,不仅可以传 int,还可以是 char,若没找到返回 -1。 | 
|  int | lastIndexOf(int ch, int fromIndex)返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 | 
|  int | lastIndexOf(String str)返回指定子字符串在此字符串中最右边出现处的索引。 | 
|  int | lastIndexOf(String str, int fromIndex)返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 | 
|  int | length()返回此字符串的长度。 | 
|  boolean | matches(String regex)告知此字符串是否匹配给定的正则表达式。 | 
|  int | offsetByCodePoints(int index, int codePointOffset)返回此String中从给定的index处偏移codePointOffset个代码点的索引。 | 
|  boolean | regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)测试两个字符串区域是否相等。 | 
|  boolean | regionMatches(int toffset, String other, int ooffset, int len)测试两个字符串区域是否相等。 | 
|  String | replace(char oldChar, char newChar)返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的。 | 
|  String | replace(CharSequence target, CharSequence replacement)使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。 | 
|  String | replaceAll(String regex, String replacement)使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 | 
|  String | replaceFirst(String regex, String replacement)使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 | 
|  String[] | split(String regex)根据给定正则表达式的匹配拆分此字符串。 | 
|  String[] | split(String regex, int limit)根据匹配给定的正则表达式来拆分此字符串。 | 
|  boolean | startsWith(String prefix)测试此字符串是否以指定的前缀开始。 | 
|  boolean | startsWith(String prefix, int toffset)测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 | 
|  CharSequence | subSequence(int beginIndex, int endIndex)返回一个新的字符序列,它是此序列的一个子序列,返回内容为 [ beginIndex , endIndex )。 | 
|  String | substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串。 | 
|  String | substring(int beginIndex, int endIndex)返回一个新字符串,它是此字符串的一个子字符串。 | 
|  char[] | toCharArray()将此字符串转换为一个新的字符数组。 | 
|  String | toLowerCase()使用默认语言环境的规则将此String中的所有字符都转换为小写。 | 
|  String | toLowerCase(Locale locale)使用给定Locale的规则将此String中的所有字符都转换为小写。 | 
|  String | toString()返回此对象本身(它已经是一个字符串!)。 | 
|  String | toUpperCase()使用默认语言环境的规则将此String中的所有字符都转换为大写。 | 
|  String | toUpperCase(Locale locale)使用给定Locale的规则将此String中的所有字符都转换为大写。 | 
|  String | trim()返回字符串的副本,忽略前导空白和尾部空白。 | 
| static String | valueOf(boolean b)返回boolean参数的字符串表示形式。 | 
| static String | valueOf(char c)返回char参数的字符串表示形式。 | 
| static String | valueOf(char[] data)返回char数组参数的字符串表示形式。 | 
| static String | valueOf(char[] data, int offset, int count)返回char数组参数的特定子数组的字符串表示形式。 | 
| static String | valueOf(double d)返回double参数的字符串表示形式。 | 
| static String | valueOf(float f)返回float参数的字符串表示形式。 | 
| static String | valueOf(int i)返回int参数的字符串表示形式。 | 
| static String | valueOf(long l)返回long参数的字符串表示形式。 | 
| static String | valueOf(Object obj)返回Object参数的字符串表示形式。 | 
常用方法
charAt(int index) 索引;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String();str = "ABCDEFG";System.out.println(str.charAt(4));  //E}
}
打印效果如下:

compareTo(String anotherString) 比较;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str1 = new String();String str2 = new String();str1 = "ABCDEF";str2 = "abcdef";System.out.println(str1.compareTo(str2));  // -32 //A的ASCII码为65,a的ASCII码为97,又因为String是引用数据类型,比较的是首地址,即首字母}
}
打印效果如下:

compareToIgnoreCase(String str) 忽视大小写的比较;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str1 = new String();String str2 = new String();str1 = "ABCDEF";str2 = "abcdef";System.out.println(str1.compareToIgnoreCase(str2));  // 0 }
}
打印效果如下:

contains(CharSequence s) 是否包含;
package top.sharehome.Bag;public class Method {public static void main(String[] args) {String str = new String();str = "ABCDEF";System.out.println(str.contains("A"));  //trueSystem.out.println(str.contains("BCE"));//false}
}
打印效果如下:

getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 从字符串内复制内容到另一字符数组;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String();str = "ABCDEF";char[] c = {'a','b','c','d','e','f'};str.getChars(0, 4, c, 0);System.out.println(c);  //ABCDef}
}
打印效果如下:

indexOf(int ch) 索引;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String();str = "ABCDEF";System.out.println(str.indexOf('B'));System.out.println(str.indexOf("C"));System.out.println(str.indexOf(68)); //ASCII码中68是D}
}
打印效果如下:

lastIndexOf(int ch) 索引;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String();str = "ABCDEF";System.out.println(str.lastIndexOf('E'));System.out.println(str.lastIndexOf("F"));System.out.println(str.lastIndexOf(68)); //ASCII码中68是D}
}
打印效果如下:

isEmpty() 检验是否为空;
package top.sharehome.Bag;public class Method {public static void main(String[] args) {String str1 = new String();str1 = "ABCDEF";String str2 = new String();str2 = "";System.out.println(str1.isEmpty()); //falseSystem.out.println(str2.isEmpty()); //ture}
}
打印效果如下:

split(String regex, int limit) 分割;
package top.sharehome.Bag;import java.util.Arrays;public class Demo {public static void main(String[] args) {String str = new String();str = "Anger begins with folly and ends in repentance.";String[] split = str.split(" ",4);System.out.println(Arrays.toString(split));}
}
打印效果如下:

subSequence(int beginIndex, int endIndex) 选取序列
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String();str = "ABCDEF";CharSequence subSequence = str.subSequence(1, 5);System.out.println(subSequence);}
}
打印效果如下:

trim() 剔除收尾空格
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String();str = "        A  B  C  D  E  F       ";String trim = str.trim();System.out.println(trim);}
}
打印效果如下:

String面试题
判断以下打印结果:
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = "abc";String str1 = "abc";System.out.println(str == str1);  //ⅠString str2 = new String("abc");System.out.println(str == str2);  //ⅡString str3 = "abc1";String str4 = "abc"+1;System.out.println(str3 == str4); //Ⅲint i = 1;String str5 = "abc"+i;System.out.println(str3 == str5); //Ⅳfinal int j = 1;String str6 = "abc"+j;System.out.println(str3 == str6); //Ⅴ}
}
分析:
- 当引用数据类型用 “==” 比较的时候,比较的是地址值;
- Ⅰ:由于常量 str = “abc” 已经被创建在内存当中,所以 “abc” 已经存在一个地址,当 str1 去指向它时,所指地址值和 str 地址值相同,即 str 和 str1 的指向地址相同,故 true;
- Ⅱ:str = “abc” 已经存在一个地址,而对象的建立是从一个新的地址开始建立的,也就是说当编译 String str2 = new String("abc");时,对象里有两个地址,内容均为 “abc”,故 false;
- Ⅲ:常量 str3 = “abc1” 已经被创建在内存中,而 String str4 = "abc"+1;编译时先运算,再存储,所以运算后和Ⅰ情况相同,故 true;
- Ⅳ:此时的 i 是变量,当 i 确定时就会产生一个地址,当该地址值内容和另一个 “abc” 进行运算时会当成一个新的地址,随即产生,所以运算后和Ⅱ情况相同,故 false;
- Ⅴ:此时的 j 是常量,情况和Ⅱ相同,故 true;
打印效果如下:

StringBuffer类
中文解释:字符缓冲区,java 中的缓冲区就是数组;
String 的特点:除了重新赋值外,值不可变,因为 String 的底层是一个字符数组,数组是固定长度;
StringBuffer 的特点:是一个可变长度的字符串,空参的初始容量是 16B,非空参的初始容量是 数据类型容量+16B (若该值不是 16 的倍数,则向上补充至最近的十六的倍数);
StringBuffer 有线程安全的特点,但是速度慢;
比较举例如下:
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {String str = new String("ABCDEF"); //String 的创建StringBuffer sb = new StringBuffer("abcdef"); //StringBuffer 的创建System.out.println(str);System.out.println(sb);}
}
打印效果如下:

StringBuffer构造器
| 构造方法摘要 | 
|---|
| StringBuffer()构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。 | 
| StringBuffer(CharSequence seq)public java.lang.StringBuilder(CharSequence seq) 构造一个字符串缓冲区,它包含与指定的CharSequence相同的字符。 | 
| StringBuffer(int capacity)构造一个不带字符,但具有指定初始容量的字符串缓冲区。 | 
| StringBuffer(String str)构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。 | 
StringBuffer方法
| 返回类型 | 方法解释 | 
|---|---|
| StringBuffer | append(boolean b)将boolean参数的字符串表示形式追加到序列。 | 
|  StringBuffer | append(char c)将char参数的字符串表示形式追加到此序列。 | 
|  StringBuffer | append(char[] str)将char数组参数的字符串表示形式追加到此序列。 | 
|  StringBuffer | append(char[] str, int offset, int len)将char数组参数的子数组的字符串表示形式追加到此序列。 | 
|  StringBuffer | append(CharSequence s)将指定的CharSequence追加到该序列。 | 
|  StringBuffer | append(CharSequence s, int start, int end)将指定CharSequence的子序列追加到此序列。 | 
|  StringBuffer | append(double d)将double参数的字符串表示形式追加到此序列。 | 
|  StringBuffer | append(float f)将float参数的字符串表示形式追加到此序列。 | 
|  StringBuffer | append(int i)将int参数的字符串表示形式追加到此序列。 | 
|  StringBuffer | append(long lng)将long参数的字符串表示形式追加到此序列。 | 
|  StringBuffer | append(Object obj)追加Object参数的字符串表示形式。 | 
|  StringBuffer | append(String str)将指定的字符串追加到此字符序列。 | 
|  StringBuffer | append(StringBuffer sb)将指定的StringBuffer追加到此序列中。 | 
|  StringBuffer | appendCodePoint(int codePoint)将codePoint参数的字符串表示形式追加到此序列。 | 
|  int | capacity()返回当前容量。 | 
|  char | charAt(int index)返回此序列中指定索引处的char值。 | 
|  int | codePointAt(int index)返回指定索引处的字符(统一代码点)。 | 
|  int | codePointBefore(int index)返回指定索引前的字符(统一代码点)。 | 
|  int | codePointCount(int beginIndex, int endIndex)返回此序列指定文本范围内的统一代码点。 | 
|  StringBuffer | delete(int start, int end)移除此序列的子字符串中的字符。 | 
|  StringBuffer | deleteCharAt(int index)移除此序列指定位置的char。 | 
|  void | ensureCapacity(int minimumCapacity)确保容量至少等于指定的最小值。 | 
|  void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)将字符从此序列复制到目标字符数组dst。 | 
|  int | indexOf(String str)返回第一次出现的指定子字符串在该字符串中的索引。 | 
|  int | indexOf(String str, int fromIndex)从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。 | 
|  StringBuffer | insert(int offset, boolean b)将boolean参数的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int offset, char c)将char参数的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int offset, char[] str)将char数组参数的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int index, char[] str, int offset, int len)将数组参数str的子数组的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int dstOffset, CharSequence s)将指定CharSequence插入此序列中。 | 
|  StringBuffer | insert(int dstOffset, CharSequence s, int start, int end)将指定CharSequence的子序列插入此序列中。 | 
|  StringBuffer | insert(int offset, double d)将double参数的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int offset, float f)将float参数的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int offset, int i)将int参数的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int offset, long l)将long参数的字符串表示形式插入此序列中。 | 
|  StringBuffer | insert(int offset, Object obj)将Object参数的字符串表示形式插入此字符序列中。 | 
|  StringBuffer | insert(int offset, String str)将字符串插入此字符序列中。 | 
|  int | lastIndexOf(String str)返回最右边出现的指定子字符串在此字符串中的索引。 | 
|  int | lastIndexOf(String str, int fromIndex)返回最后一次出现的指定子字符串在此字符串中的索引。 | 
|  int | length()返回长度(字符数)。 | 
|  int | offsetByCodePoints(int index, int codePointOffset)返回此序列中的一个索引,该索引是从给定index偏移codePointOffset个代码点后得到的。 | 
|  StringBuffer | replace(int start, int end, String str)使用给定String中的字符替换此序列的子字符串中的字符。 | 
|  StringBuffer | reverse()将此字符序列用其反转形式取代。 | 
|  void | setCharAt(int index, char ch)将给定索引处的字符设置为ch。 | 
|  void | setLength(int newLength)设置字符序列的长度。 | 
|  CharSequence | subSequence(int start, int end)返回一个新的字符序列,该字符序列是此序列的子序列。 | 
|  String | substring(int start)返回一个新的String,它包含此字符序列当前所包含的字符子序列。 | 
|  String | substring(int start, int end)返回一个新的String,它包含此序列当前所包含的字符子序列。 | 
|  String | toString()返回此序列中数据的字符串表示形式。 | 
|  void | trimToSize()尝试减少用于字符序列的存储空间。 | 
常用方法
capacity() 返回当前容量;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {StringBuffer buf = new StringBuffer();System.out.println(buf.capacity());}
}
打印效果如下:

length() 返回长度(字符数);
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {StringBuffer buf = new StringBuffer("abc");System.out.println(buf.length());}
}
打印效果如下:

append() 添加;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {StringBuffer buf = new StringBuffer("abcdefghijklmnop");String str = "abcdefghijklmnop";buf.append(1234);System.out.println(buf);buf.append(str);System.out.println(buf);}
}
打印效果如下:

delete(int start, int end) 删除;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {StringBuffer buf = new StringBuffer("abcdefghijklmnop");String str = "abcdefghijklmnop";buf.append(1234);System.out.println(buf);buf.append(str);System.out.println(buf);buf.delete(16, 20);  //删除1234System.out.println(buf);}
}
打印效果如下:

insert(int offset, ) 插入;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {StringBuffer buf = new StringBuffer("abc");buf.insert(1, "ABC");System.out.println(buf);}
}
打印效果如下:

reverse() 反转;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {StringBuffer buf = new StringBuffer("abc");buf.reverse();System.out.println(buf);}
}
打印效果如下:

replace(int start, int end, String str) 替换;
package top.sharehome.Bag;public class Demo {public static void main(String[] args) {StringBuffer buf = new StringBuffer("abc");buf.replace(2, 3, "ABC");System.out.println(buf);}
}
打印效果如下:

StringBuilder类
StringBuilder 类和StringBuffer 类的构造器和方法一样;
区别是StringBuilder 类是线程不安全的,但是速度快;
所以在不考虑多线程的环境下,用StringBuilder 类,因为快,在考虑多线程的情况下使用 StringBuffer 类,因为安全;