Java常用类库:字符串相关类——Class String、Class StringBuffer、Class StringBuilder
1、Class String
1.1、String类的介绍
所有在Java程序中定义的字符串都是Class String 的实例,因为在定义字符串时使用 String 来进行声明。由于String类声明的字符串在创建之后是不可变的,也就是说字符串中的值是无法更改的,因此多个内容相等的字符串对象是共享同一个地址空间的。
public class Test{
public static void main(String[] args){
String a = "abc";
String b = "abc";
System.out.printfln(a==b);
}
}
该结果会输出true,表示这两个字符串对象a和b是公用一个内存(“==“在字符串中进行的是内存地址的比较),当然如果是使用new字段创建的字符串,尽管内容相等,但是存储的内存地址是不同的,因为使用new创建的对象都会存储在一个独立的空间。
在Java中如果“+”号的任意一边是字符串类型。那么“+”号就不再是运算符号,而是字符串拼接符号,就比如以下例子:
public class Test{
public static void main(String[] args){
int a = 1;
String b = "abc";
System.out.printfln(a+b);
}
}
最后输出的结果是:1abc,这就是实现了字符串的拼接。
字符串在定义之后是储存在字符串常量池中,而字符串常量池在如今的Java版本中被移动到了堆内存的永久代中,在永久代中存储的任何实例是无法被GC(自动垃圾回收),因此字符串是无法被GC的。由于这个特性,在项目中要注意:尽量不要进行大量的字符串拼接,因为字符串是不可变的,在拼接了新的字符串之后,会生成新的字符串,之前的旧字符串如果在之后的代码中没有引用,那么这些旧字符串就会变为内存垃圾,而字符串是不会被GC的,就导致内存的占用。那想要进行字符串的拼接,该使用哪种方式呢,先卖个关子,在介绍完字符串常用方法之后,告诉大家。
1.2、String类的构造方法
创建字符串需要用到该类的构造方法,String类比较常用的构造方法有以下几种:
- Sring a =" " ——该方式是在编写程序中最常用的
- String b = new String() ——该方式创建的是一个新内存地址的空字符串,与上面的方式是由区别的
- String c = new String(char[ ] value) ——创建一个新的字符串,使其包含char类型的数组的字符序列
- String d = new String(byte[ ] value) ——使用平台默认的字符集的字节数组构造一个新的字符串
- String e = new String(StringBuffer buffer) ——分配一个新字符串,其中包含当前包含在字符串缓冲区参数中的字符序列。
- String f = new String(StringBuilder builder) ——分配一个新字符串,其中包含当前包含在字符串构建器参数中的字符序列。
以上的几种构造方法只需要掌握第一和第二个就可以了,之后的几个作为了解,在项目中按需求来选择是否使用。
1.3、String类的常用方法
String类中常用的方法有以下几种:
- charAt方法
- compareTo方法
- compareToIgnoreCase方法
- concat方法
- replace方法
- valueOf方法
- indexOf方法
- trim方法
- substring方法
- strip方法
- length方法
- isEmpty方法
- equals方法
- endsWith方法
- startWith方法
(1)第一个方法的完整格式:char charAt(int index),该方法的作用是:返回指定的索引处的char值,传入的参数为int类型,表示指定的索引,注意:索引的初始值为0,在使用方法时传入的参数的值不可以小于0或大于(字符串长度-1),不然抛出异常(IndexOutOfBoundsException )以下是代码示例:
public class Demo1 {
public static void main(String[] args) {
String a = "abcde";
System.out.println(a.charAt(1));
}
}
该运行结果为:
因为我传入的索引值为1,所以返回的是字符串中的第二个字符b
(2)第二个方法的完整格式:int compareTo(String str),该方法的作用是:按照字典顺序(Unicode值)来比较两个字符串,使用该方法是传入另一个字符串对象,比较结果当两个字符串的顺序相同,返回0,调用方法的字符串字典顺序小于传入的字符串,返回小于0的数,调用方法的字符串字典顺序大于传入的字符串,返回大于0的数,代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "abcde";
String b = "abcde";
String c = "dacda";
System.out.println(a.compareTo(b));
System.out.println(a.compareTo(c));
System.out.println(c.compareTo(a));
}
}
运行效果如下:
因为对象a和对象b的值是一样的,所以返回的是0,而对象a和对象c在第一个字符上的字典顺序就不一样了,又因为对象a的第一个字符是a,而c对象的第一个字符是d,字符a比字符d的字典顺序小三为所以返回的是-3和3
(3)第三个方法的完整格式为:int compareToIgnoreCase(String str),该方法的作用和上述的compareTo方法一致,只是compareToIgnoreCase方法是忽略字母的大小写进行比较的,具体如何说是忽略大小写的比较,下面的代码可以很好的展示:
public class Demo1 {
public static void main(String[] args) {
String a = "abcd";
String b = "aBcd";
System.out.println(a.compareTo(b));
System.out.println(a.compareToIgnoreCase(b));
}
}
以下是运行效果:
可以看出对象a和对象b中的b字母一个是小写,一个是大写,在使用compareTo方法返回的结果是32,说明字符顺序是不同的,而使用了compareToIgnoreCase方法返回的结果是0,这就说明了是忽略了字母大小写问题。
(4)第四个方法完整的格式:String concat(Sting str),该方法的作用是:将指定的字符串连接到此字符串的尾部,使用方法时传入一个字符串对象。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "abc";
String b = "def";
System.out.println(a.concat(b));
}
}
运行效果如下:
(5)第五个方法的完整格式:String replace(char old,char new),该方法的作用是:将字符串中每个与参数old值相同的字符替换成参数new的值,代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "abbde";
System.out.println(a.replace('b', 'c'));
}
}
运行的效果:
可以看到对象a中的所有字符b都替换成了字符c
(6)第六种方法的完整格式:String valueOf(Object o),该方法的作用是:返回Object参数的字符串表示形式。以下是代码:
public class Demo1 {
public static void main(String[] args) {
int a = 125;
char[] b = {'a','b','c'};
String c = "";
System.out.println(c.valueOf(a));
System.out.println(c.valueOf(b));
}
}
运行的效果图:
可以看到是可以传入int类型这些八大基本类型、也可以传入数组这种引用类型,因为Object是任何类的父类,这样就可以将不是字符串类型的值转化成字符串类型。
(7)第七种方法的完整格式:int indexOf(String str),该方法的作用是:返回字符串中的第一次出现指定子字符串的索引,使用方法时传入一个子字符串。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "ab@de";
System.out.println(a.indexOf("@"));
System.out.println(a.indexOf("f"));
}
}
运行效果如下:
"@"符号在对象a中是第三个,所以索引值为2,而字符“f”在对象a中不存在,返回的是-1。
(8)第八种方法的完整格式:String trim(),该方法的作用是:去除字符串中前后端的空格。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = " abcff ";
System.out.print(a);
System.out.println(a);
System.out.print(a.trim());
System.out.print(a.trim());
}
}
运行效果如下:
从输出效果很明显可以看出:去除了前端空格和没去除前端空格的差异。
(9)第九种方法的完整格式:String substring(int startIndex,int endIndex),该方法的作用是:按指定的开始索引和结束索引截取字符串。传入的第一个参数为截取的开始索引,第二个参数为截取的结束索引。注意:截取的范围是包括开始索引而不包括结束索引,而且第一个参数不可以小于0,第二个参数不可以大于字符串的长度。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "acbggd";
System.out.println(a.substring(1,5));
}
}
运行效果如下:
可以看到截取的范围确实是从索引值为1的字符处开始截取,到索引值为5的字符前结束。
(10)第十个方法的完整格式为:String strip(),该方法的作用是:去除字符串中前端和后端的空白符。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = " abcff ";
System.out.print(a);
System.out.println(a);
System.out.print(a.strip());
System.out.print(a.strip());
}
}
运行的代码如下:
可以看到该方法和trim方法的效果是一致的。
(11)第十一个方法的完整格式为:int length(),该方法的作用是:返回字符串的长度。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "asddda";
System.out.println(a.length());
}
}
运行效果如下:
(12)第十二个方法的完整格式:boolean isEmpty(),该方法的作用是:判断字符串是否为空,为空返回true,不为空返回false。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "";
String b = "a";
System.out.println(a.isEmpty());
System.out.println(b.isEmpty());
}
}
运行效果如下:
(13)第十三个方法的完整格式:boolean equals(String str),该方法的作用是:判断调用方法的字符串的值是否与传入的字符串的值相等,相等返回true,不相等返回false。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "abc";
String b = "abc";
String c = "bcd";
System.out.println(a.equals(b));
System.out.println(a.equals(c));
}
}
运行效果如下:
(14)第十四个方法的完整格式:boolean endsWith(String str),该方法的作用是:判断字符串是否是以指定的字符结尾,是返回true,不是返回false。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "acbbd";
System.out.println(a.endsWith("d"));
System.out.println(a.endsWith("b"));
}
}
运行效果如下:
(15)第十五个方法的完整格式:boolean startsWith(String str),该方法的作用是:判断字符串是否是以指定的字符开头,是返回true,不是返回false。代码如下:
public class Demo1 {
public static void main(String[] args) {
String a = "acbbd";
System.out.println(a.startsWith("a"));
System.out.println(a.startsWith("b"));
}
}
运行效果如下:
好了关于String类的常用方法就已经介绍完毕,那就来揭晓上面留下的悬念:该如何进行字符串的拼接,使用到其他两个与字符串拼接操作相关的类:StringBuffer类和StringBuilder类。
2、Class StringBuffer和Class StringBuilder
2.1、两个类的区别
StringBuffer和StringBuilder这两个类与String类最大的不同之处就是StringBuffer和StringBuilder这两个类创建的对象的长度是可变的。那StringBuffer类和StringBuilder类有何区别呢,它们之间的区别在于StringBuffer在多线程的程序中是同步执行的,使用该类存储的数据安全性高,但执行操作时性能较低,而StringBuilder在多线程的程序中是异步执行的,使用该类存储数据的安全性低,但执行操作时性能较高。
2.2、两个类的构造方法
(1)StringBuffer类常用的构造方法如下:
- StringBuffer a = new StringBuffer()——构造一个字符串缓冲区,其中没有字符,初始容量为16个字符。
- StringBuffer b = new StringBuffer(int length)——构造一个字符串缓冲区,其中没有字符,并指定length的值为初始容量。
- StringBuffer c = new StringBuffer(String str)——构造一个初始化为指定字符串内容的字符串缓冲区。
(2)StringBuilder类常用的构造方法如下:
- StringBuilder a = new StringBuilder()——构造一个字符串构建器,其中没有字符,初始容量为16个字符。
- StringBuilder b = new StringBuilder(int length)——构造一个字符串构建器,其中没有字符,并指定length的值为初始容量。
- StringBuilder c = new StringBuilder(String str)——构造一个初始化为指定字符串内容的字符串缓冲区。
2.3、两个类常用的方法
StringBuffer和StringBuilder两个类的对象常用的方法是一样的,因此就一起介绍了。这两个类的对象可以调用的方法有以下几个:
- append方法
- charAt方法
- indexOf方法
- lastIndexOf方法
- insert方法
- delete方法
- deletCharAt方法
- length方法
- capacity方法
- replace方法
- reverse方法
- substring方法
- setCharAt方法
- setLength方法
- trimToSize方法
(1)第一个方法的完整格式:StringBuilder / StringBuffer append(Object o),该方法的作用为:将指定的Object参数追加到此字符序列。 代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 150;
String d = "abc";
a.append(c);
a.append(d);
b.append(d);
b.append(c);
System.out.println(a);
System.out.println(b);
}
}
运行效果如下:
(2)第二个方法完整格式:char charAt(int index),该方法的这作用是:返回指定索引处的此序列中的 char值。注意:参数的值不可以小于0,运不可以大于序列的长度,不然会抛出异常(IndexOutOfBoundsException ),代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 150;
String d = "abc";
a.append(c);
a.append(d);
b.append(d);
b.append(c);
System.out.println(a);
System.out.println(a.charAt(2));
System.out.println(b);
System.out.println(b.charAt(4));
}
}
运行效果如下:
(3)第三个方法的完整格式:int indexOf(String str),该方法的作用是:返回指定子字符串第一次出现的字符串中的索引,不存在指定字符串返回-1。代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 150;
String d = "abc";
a.append(c);
a.append(d);
b.append(d);
b.append(c);
System.out.println(a);
System.out.println(a.indexOf("a"));
System.out.println(b);
System.out.println(b.indexOf("a"));
}
}
运行效果如下:
(4)第四个方法的完整格式:int lastIndexOf(String str),该方法的作用是:返回指定子字符串最后一次出现的字符串中的索引,如果不存在指定的字符串返回-1。代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abca";
a.append(c);
a.append(d);
b.append(d);
b.append(c);
System.out.println(a);
System.out.println(a.lastIndexOf("a"));
System.out.println(b);
System.out.println(b.lastIndexOf("a"));
}
}
运行效果如下:
(5)第五个方法的完整格式:StringBuffer / StringBuilder insert(int offset,Object o),该方法的作用是:将Object参数的字符串表示形式插入到此字符序列指定的索引处。注意:第一个参数为索引值,而索引的值不可以小于0或者是大于序列的长度,不然会抛出异常(StringIndexOutOfBoundsException )。代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abca";
a.append(c);
System.out.println(a);
a.insert(2,d);
System.out.println(a);
b.append(d);
System.out.println(b);
b.insert(3,c);
System.out.println(b);
}
}
运行的效果如下:
(6)第六个方法的完整格式:StringBuffer / StringBuilder delete (int startIndex,int endIndex),该方法的作用为:删除此序列的子字符串中的字符。 子字符串从指定的startIndex开始,并且如果不存在这样的字符,则扩展到索引endIndex- 1处的字符或序列的末尾。 如果startIndex等于endIndex,则不进行任何更改。 注意: 如果 start为负数,大于 序列的长度 或大于 endIndex 会抛出异常(StringIndexOutOfBoundsException )。代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abca";
a.append(c);
a.insert(2,d);
System.out.println(a);
System.out.println(a.delete(0, 2));
b.append(d);
b.insert(3,c);
System.out.println(b);
System.out.println(b.delete(1, 3));
}
}
运行后的效果:
(7)第七个方法的完整格式:StringBuilder / StringBuffer deleteCharAt(int index),该方法的作用是:按此顺序删除指定位置的char 。注意: 如果 index为负数或大于或等于序列的长度会抛出异常(StringIndexOutOfBoundsException),代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abca";
a.append(c);
a.insert(2,d);
System.out.println(a);
System.out.println(a.deleteCharAt(2));
b.append(d);
b.insert(3,c);
System.out.println(b);
System.out.println(b.deleteCharAt(3));
}
}
运行的效果如下:
(8)第八个方法的完整格式:int length(),该方法的作用是:返回序列的长度,代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abca";
a.append(c);
a.insert(2,d);
System.out.println(a.length());
b.append(d);
b.insert(3,c);
System.out.println(b.length());
}
}
运行效果如下:
(9)第九个方法的完整格式:int capacity(),方法的作用是:返回当前的容量。代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abca";
a.append(c);
a.insert(2,d);
System.out.println(a.capacity());
b.append(d);
b.insert(3,c);
System.out.println(b.capacity());
}
}
运行效果如下:
会不会有小伙伴有疑问:为什么是16,这个16哪里来的。这个16是创建StringBuffer或者是StringBuilder对象是传入的默认容量,在上面介绍两个类的构造方法时提及了,忘记的小伙伴可以翻上去看看。
(10)第十个方法完整的格式:StringBuffer / StringBuilder replace(int startIndex,int endIndex,String str),该方法的作用是:使用指定的String的字符替换此序列的子字符串中的字符。 子字符串从指定的start开始,如果不存在这样的字符,则扩展到索引end - 1处的字符或序列的末尾。 首先删除子字符串中的字符,然后在String插入指定的start 。 注意:如果 startIndex为负数,大于序列长度或大于 endIndex会抛出异常(StringIndexOutOfBoundsException) ,代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "ab";
a.append(c);
System.out.println(a);
a.replace(1, 3, d);
System.out.println(a);
b.append(c);
System.out.println(b);
b.replace(2, 4, d);
System.out.println(b);
}
}
运行效果如下:
(11)第十一个方法的完整格式:StringBuffer / StringBuilder reverse(),该方法的作用是:导致此字符序列被序列的反向替换。代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abcd";
a.append(c);
a.append(d);
System.out.println(a);
System.out.println(a.reverse());
b.append(d);
b.append(c);
System.out.println(b);
System.out.println(b.reverse());
}
}
运行效果如下:
(12)第十二个方法的完整格式:String substring(int startIndex,int endIndex),该方法的作用是:返回一个新的String ,其中包含当前包含在此序列中的字符的子序列。 子字符串从指定的startIndex开始,并扩展到索引endIndex- 1处的字符。 注意:如果 startIndex或 endIndex为负数或大于序列长度 ,或者 startIndex大于 endIndex会抛出异常(StringIndexOutOfBoundsException)。代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abcd";
a.append(c);
a.append(d);
System.out.println(a);
System.out.println(a.substring(3,6));
b.append(d);
b.append(c);
System.out.println(b);
System.out.println(b.substring(1,4));
}
}
运行效果如下:
(13)第十三个方法的完整格式:void setCharAt(int index,char a),该方法的作用是:将指定索引处的字符设置为a的值 。注意: 如果 index为负数或大于或等于序列长度会抛出异常(IndexOutOfBoundsException ),代码如下:
public class Demo1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer();
StringBuilder b = new StringBuilder();
int c = 1500;
String d = "abcd";
a.append(c);
a.append(d);
System.out.println(a);
a.setCharAt(2,'@');
System.out.println(a);
b.append(d);
b.append(c);
System.out.println(b);
b.setCharAt(2,'#');
System.out.println(b);
}
}
运行效果如下:
好了关于字符串相关的知识就介绍到这,希望文章可以帮到你们,谢谢阅读。如果觉得文章写得不错,可以关注我,我会不定期更新关于Java相关的知识博文。