由浅到深认识Java语言(19):String类

该文章Github地址:https://github.com/AntonyCheng/java-notes【有条件的情况下推荐直接访问GitHub以获取最新的代码更新】

在此介绍一下作者开源的SpringBoot项目初始化模板(Github仓库地址:https://github.com/AntonyCheng/spring-boot-init-template【有条件的情况下推荐直接访问GitHub以获取最新的代码更新】& 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方法

返回类型方法解释
charcharAt(int index) 返回指定索引处的 char 值。
intcodePointAt(int index) 返回指定索引处的字符(Unicode 代码点)。
intcodePointBefore(int index) 返回指定索引之前的字符(Unicode 代码点)。
intcodePointCount(int beginIndex, int endIndex) 返回此 String 的指定文本范围中的 Unicode 代码点数。
intcompareTo(String anotherString) 按字典顺序比较两个字符串,返回值是两字符的字符数之间的差值(大小写需相同)
intcompareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写,返回值是两字符的字符数之间的差值
Stringconcat(String str) 将指定字符串连接到此字符串的结尾。
booleancontains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true,以接口方式存在,以多态方式运用
booleancontentEquals(CharSequence cs) 将此字符串与指定的 CharSequence 比较。
booleancontentEquals(StringBuffer sb) 将此字符串与指定的 StringBuffer 比较。
static StringcopyValueOf(char[] data) 返回指定数组中表示该字符序列的 String。
static StringcopyValueOf(char[] data, int offset, int count) 返回指定数组中表示该字符序列的 String。
booleanendsWith(String suffix) 测试此字符串是否以指定的后缀结束。
booleanequals(Object anObject) 将此字符串与指定的对象比较。
booleanequalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写。
static Stringformat(Locale l, String format, Object... args) 使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
static Stringformat(String format, Object... args) 使用指定的格式字符串和参数返回一个格式化字符串。
byte[]getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
byte[]getBytes(Charset charset) 使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
voidgetBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) 已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。
byte[]getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此字符串复制到目标字符数组,参数为(源字符串复制起始位,源字符串字符个数,目标字符串,目标字符串开始复制位置)
inthashCode() 返回此字符串的哈希码。
intindexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引,此时的参数 int 也运用了多态,不仅可以传 int,还可以是 char,若没找到返回 -1
intindexOf(int ch, int fromIndex) 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
intindexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
intindexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
Stringintern() 返回字符串对象的规范化表示形式。
booleanisEmpty() 当且仅当 length()0 时返回 true
intlastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引,此时的参数 int 也运用了多态,不仅可以传 int,还可以是 char,若没找到返回 -1
intlastIndexOf(int ch, int fromIndex) 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
intlastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
intlastIndexOf(String str, int fromIndex) 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
intlength() 返回此字符串的长度。
booleanmatches(String regex) 告知此字符串是否匹配给定的正则表达式。
intoffsetByCodePoints(int index, int codePointOffset) 返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。
booleanregionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 测试两个字符串区域是否相等。
booleanregionMatches(int toffset, String other, int ooffset, int len) 测试两个字符串区域是否相等。
Stringreplace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
Stringreplace(CharSequence target, CharSequence replacement) 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
StringreplaceAll(String regex, String replacement) 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
StringreplaceFirst(String regex, String replacement) 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
String[]split(String regex) 根据给定正则表达式的匹配拆分此字符串。
String[]split(String regex, int limit) 根据匹配给定的正则表达式来拆分此字符串。
booleanstartsWith(String prefix) 测试此字符串是否以指定的前缀开始。
booleanstartsWith(String prefix, int toffset) 测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
CharSequencesubSequence(int beginIndex, int endIndex) 返回一个新的字符序列,它是此序列的一个子序列,返回内容为 [ beginIndex , endIndex )
Stringsubstring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
Stringsubstring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
char[]toCharArray() 将此字符串转换为一个新的字符数组。
StringtoLowerCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
StringtoLowerCase(Locale locale) 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
StringtoString() 返回此对象本身(它已经是一个字符串!)。
StringtoUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
StringtoUpperCase(Locale locale) 使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
Stringtrim() 返回字符串的副本,忽略前导空白和尾部空白。
static StringvalueOf(boolean b) 返回 boolean 参数的字符串表示形式。
static StringvalueOf(char c) 返回 char 参数的字符串表示形式。
static StringvalueOf(char[] data) 返回 char 数组参数的字符串表示形式。
static StringvalueOf(char[] data, int offset, int count) 返回 char 数组参数的特定子数组的字符串表示形式。
static StringvalueOf(double d) 返回 double 参数的字符串表示形式。
static StringvalueOf(float f) 返回 float 参数的字符串表示形式。
static StringvalueOf(int i) 返回 int 参数的字符串表示形式。
static StringvalueOf(long l) 返回 long 参数的字符串表示形式。
static StringvalueOf(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"));  //true
		System.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()); //false
		System.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); //Ⅴ
	}
}

分析:

  1. 当引用数据类型用 “==” 比较的时候,比较的是地址值;
  2. Ⅰ:由于常量 str = “abc” 已经被创建在内存当中,所以 “abc” 已经存在一个地址,当 str1 去指向它时,所指地址值和 str 地址值相同,即 str 和 str1 的指向地址相同,故 true;
  3. Ⅱ:str = “abc” 已经存在一个地址,而对象的建立是从一个新的地址开始建立的,也就是说当编译 String str2 = new String("abc"); 时,对象里有两个地址,内容均为 “abc”,故 false;
  4. Ⅲ:常量 str3 = “abc1” 已经被创建在内存中,而 String str4 = "abc"+1; 编译时先运算,再存储,所以运算后和Ⅰ情况相同,故 true;
  5. Ⅳ:此时的 i 是变量,当 i 确定时就会产生一个地址,当该地址值内容和另一个 “abc” 进行运算时会当成一个新的地址,随即产生,所以运算后和Ⅱ情况相同,故 false;
  6. Ⅴ:此时的 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方法

返回类型方法解释
StringBufferappend(boolean b)boolean 参数的字符串表示形式追加到序列。
StringBufferappend(char c)char 参数的字符串表示形式追加到此序列。
StringBufferappend(char[] str)char 数组参数的字符串表示形式追加到此序列。
StringBufferappend(char[] str, int offset, int len)char 数组参数的子数组的字符串表示形式追加到此序列。
StringBufferappend(CharSequence s) 将指定的 CharSequence 追加到该序列。
StringBufferappend(CharSequence s, int start, int end) 将指定 CharSequence 的子序列追加到此序列。
StringBufferappend(double d)double 参数的字符串表示形式追加到此序列。
StringBufferappend(float f)float 参数的字符串表示形式追加到此序列。
StringBufferappend(int i)int 参数的字符串表示形式追加到此序列。
StringBufferappend(long lng)long 参数的字符串表示形式追加到此序列。
StringBufferappend(Object obj) 追加 Object 参数的字符串表示形式。
StringBufferappend(String str) 将指定的字符串追加到此字符序列。
StringBufferappend(StringBuffer sb) 将指定的 StringBuffer 追加到此序列中。
StringBufferappendCodePoint(int codePoint)codePoint 参数的字符串表示形式追加到此序列。
intcapacity() 返回当前容量。
charcharAt(int index) 返回此序列中指定索引处的 char 值。
intcodePointAt(int index) 返回指定索引处的字符(统一代码点)。
intcodePointBefore(int index) 返回指定索引前的字符(统一代码点)。
intcodePointCount(int beginIndex, int endIndex) 返回此序列指定文本范围内的统一代码点。
StringBufferdelete(int start, int end) 移除此序列的子字符串中的字符。
StringBufferdeleteCharAt(int index) 移除此序列指定位置的 char
voidensureCapacity(int minimumCapacity) 确保容量至少等于指定的最小值。
voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此序列复制到目标字符数组 dst
intindexOf(String str) 返回第一次出现的指定子字符串在该字符串中的索引。
intindexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
StringBufferinsert(int offset, boolean b)boolean 参数的字符串表示形式插入此序列中。
StringBufferinsert(int offset, char c)char 参数的字符串表示形式插入此序列中。
StringBufferinsert(int offset, char[] str)char 数组参数的字符串表示形式插入此序列中。
StringBufferinsert(int index, char[] str, int offset, int len) 将数组参数 str 的子数组的字符串表示形式插入此序列中。
StringBufferinsert(int dstOffset, CharSequence s) 将指定 CharSequence 插入此序列中。
StringBufferinsert(int dstOffset, CharSequence s, int start, int end) 将指定 CharSequence 的子序列插入此序列中。
StringBufferinsert(int offset, double d)double 参数的字符串表示形式插入此序列中。
StringBufferinsert(int offset, float f)float 参数的字符串表示形式插入此序列中。
StringBufferinsert(int offset, int i)int 参数的字符串表示形式插入此序列中。
StringBufferinsert(int offset, long l)long 参数的字符串表示形式插入此序列中。
StringBufferinsert(int offset, Object obj)Object 参数的字符串表示形式插入此字符序列中。
StringBufferinsert(int offset, String str) 将字符串插入此字符序列中。
intlastIndexOf(String str) 返回最右边出现的指定子字符串在此字符串中的索引。
intlastIndexOf(String str, int fromIndex) 返回最后一次出现的指定子字符串在此字符串中的索引。
intlength() 返回长度(字符数)。
intoffsetByCodePoints(int index, int codePointOffset) 返回此序列中的一个索引,该索引是从给定 index 偏移 codePointOffset 个代码点后得到的。
StringBufferreplace(int start, int end, String str) 使用给定 String 中的字符替换此序列的子字符串中的字符。
StringBufferreverse() 将此字符序列用其反转形式取代。
voidsetCharAt(int index, char ch) 将给定索引处的字符设置为 ch
voidsetLength(int newLength) 设置字符序列的长度。
CharSequencesubSequence(int start, int end) 返回一个新的字符序列,该字符序列是此序列的子序列。
Stringsubstring(int start) 返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
Stringsubstring(int start, int end) 返回一个新的 String,它包含此序列当前所包含的字符子序列。
StringtoString() 返回此序列中数据的字符串表示形式。
voidtrimToSize() 尝试减少用于字符序列的存储空间。
常用方法

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);  //删除1234
		System.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 类,因为安全;

下一章:由浅到深认识Java语言(20):包装类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值