字符串就是一连串的字符序列,Java提供了String、StringBuffer和StringBuilder三个类来处理字符串各种行为,其中包含了诸多实用方法
String类
String类是个不可变类,即一旦创建了一个String对象,包含在这个对象中的字符序列是不可改变的,直至这个对象被销毁
在Java9之前字符串采用char[]数组保存字符,因此字符串的每个字符占2个字节,在Java9之后采用byte[]数组再加一个enencoding-flag字段来保存字符,字符串的每个字符只占1个字节,字符串的功能方法不受影响。
String类提供了大量构造器来创建String对象,常用的如下所示:
- String():创建一个包含0个字符串序列的String对象(并不返回null)
- String(byte[] bytes, Charset charset):使用指定的字符集将指定的byte[]数组解码成一个新的String对象
- String(byte[] bytes, int offset, int length):使用平台默认字符集将指定的byte[]数组从offset开始长度为length的子数组解码成一个新的String对象
- String(byte[] bytes, int offset, int length, String charsetName):使用指定的字符集将指定的byte[]数组从offset开始长度为length的自舒服解码成一个新的String对象
- String(byte[] bytes, String charsetName):使用指定的字符集将指定的byte[]数组解码成一个新的String对象
- String(char[] value, int offset, int count):将指定的字符数组从offset开始长度为count的字符元素连缀成字符串
- String(String original):根据字符串直接量创建String对象,也就是说创建的String对象是这个直接量的副本
- String(StringBuffer buffer):根据StringBuffer对象来创建对应的String对象
- String(StringBuilder builder):根据StringBuilder对象来创建对应的String对象
- char charAt(int index):获取字符串中指定位置的字符,其中参数index指的字符串的序数,从0到length-1
jshell> String str = new String("davieyang.com")
str ==> "davieyang.com"
jshell> System.out.println("The sixth char is\n" + str.charAt(5));
The sixth char is
y
- int compareTo(String anotherString):比较两个字符串大小,如果两个字符串相等则返回0,不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差。另一个情况,较长字符串的前面部分恰巧是较短的字符串,则返回他们的长度差
jshell> String str1 = new String("abcdefghijklmn");
str1 ==> "abcdefghijklmn"
jshell> String str2 = new String("abcdefghij");
str2 ==> "abcdefghij"
jshell> String str3 = new String("abcdefghijklmn");
str3 ==> "abcdefghijklmn"
jshell> System.out.println("str1 compare to str2\n" + str1.compareTo(str2));
str1 compare to str2
4
jshell> System.out.println("str1 compare to str3\n" + str1.compareTo(str3));
str1 compare to str3
0
jshell> String str3 = new String("abcdefghijalmn");
str3 ==> "abcdefghijalmn"
jshell> System.out.println("str1 compare to str3\n" + str1.compareTo(str3));
str1 compare to str3
10
- String concat(String str):将该String对象与str链接在一起,与“+”功能相同
- boolean contentEquals(StringBuffer sb):将该String对像鱼StringBuffer对象sb进行比较,字符序列相同则返回true
- static String copyValueOf(char[] data):将字符数组连缀成字符串,与String(char[] content)构造器的功能相同
- static String copyValueOf(char[], int offset, int count):将字符数组的子数组中的元素连缀成字符串,与String(char[] value, int offset, int count)构造器的功能相同
- boolean endsWith(String suffix):返回该String对象是否以suffix结尾
jshell> String str1 = "com";
str1 ==> "com"
jshell> String str2 = "davieyang.com"
str2 ==> "davieyang.com"
jshell> System.out.println("str2 ends With(str1)" + str2.endsWith(str1));
str2 ends With(str1)true
- boolean equals(Object anObject):将该字符串与指定对象比较,如果二者包含的字符序列相等,则返回true否则返回false
- boolean equalsIgnoreCase(String str):与上一个方法类似,但忽略大小写
- byte[] getBytes(): 将该String对象转换成byte数组
- void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):该方法将字符串中从srcBegin开始,到srcEnd结束的字符串复制到dst字符数组中,其中dstBegin为目标字符数组的起始复制位置
jshell> char[] str1 = {'d', 'a', 'v', 'i', 'e', 'y', 'a', 'n', 'g', 'd', 'y'};
str1 ==> char[11] { 'd', 'a', 'v', 'i', 'e', 'y', 'a', 'n', 'g', 'd', 'y' }
jshell> String str2 = new String("django");
str2 ==> "django"
jshell> str2.getChars(0,3,str1,7);
jshell> System.out.println(str1);
davieyadjay
- int indexOf(int ch): 找出ch字符在该字符串中第一次出现的位置
- int indexOf(int ch, int fromIndex):找出ch字符在该字符串中从fromIndex开始后第一次出现的位置
- int indexOf(String str):找出str子字符串在该字符串中第一次出现的位置
- int indexOf(String, int fromIndex):找出str子字符串在该字符串中从fromIndex开始后第一次出现的位置
jshell> String str1 = "www.daiveyang.com"
str1 ==> "www.daiveyang.com"
jshell> String str2 = "ya";
str2 ==> "ya"
jshell> System.out.println(str1.indexOf('y'));
9
jshell> System.out.println(str1.indexOf('y', 2));
9
jshell> System.out.println(str1.indexOf(str2));
9
- int lastIndexOf(int ch): 找出ch字符在该字符串中最后一次出现的位置
- int lastIndexOf(int ch, int fromIndex):找出ch字符在该字符串中从fromIndex开始后最后一次出现的位置
- int lastIndexOf(String str):找出子字符串str在该字符串中最后一次出现的位置
- int lastIndexOf(String str, int fromIndex):找出str子字符串在该字符串中从fromIndex开始后最后一次出现的位置
- int length(): 返回当前字符串长度
- String replace(char oldChar, char newChar):将字符串中第一个oldChar替换成newChar
- boolean startsWith(String prefix): 该String对象是否以prefix开始
- boolean startsWith(String prefix, int offset):该String对象从offset位置算起,是否以prefix开始
- String substring(int beginIndex):获取从beginIndex位置开始到结束的子字符串
- String substring(int beginIndex, int endIndex):获取从beginIndex位置开始到endIndex位置的子字符串
- char[] toCharArray():将该String对象转换成char数组
- String toLowerCase():将字符串转换成小写
- String toUpperCase():将字符串转换成大写
- static String valueOf(X x):一系列用于将基本类型值转换为String对象的方法
StringBuffer类和StringBuilder类
因为String类是不可变的,所以会额外产生很多临时变量使用StringBuffer和StringBuilder就可以避免这个问题;StringBuilder提供了一系列插入、追加、改变该字符串里包含的字符序列的方法,而StringBuffer与其用法完全相同,只是StringBuffer是线程安全的。
public class StringBuilderTest
{
public static void main(String[] args)
{
var sb = new StringBuilder();
// 追加字符串
sb.append("java");//sb = "java"
// 插入
sb.insert(0, "hello "); // sb="hello java"
// 替换
sb.replace(5, 6, ","); // sb="hello,java"
// 删除
sb.delete(5, 6); // sb="hellojava"
System.out.println(sb);
// 反转
sb.reverse(); // sb="avajolleh"
System.out.println(sb);
System.out.println(sb.length()); // 输出9
System.out.println(sb.capacity()); // 输出16
// 改变StringBuilder的长度,将只保留前面部分
sb.setLength(5); // sb="avajo"
System.out.println(sb);
}
}