String,StringBuffer与StringBuilder
1.String
不是基本数据类型而是一个类,被用来表示字符序列。
1)String的特点:一旦赋值就不能更改其指向的字符对象。每次改变都是产生了一个新的对象。(所以其凭借效率不高,每次拼接都要产生新的是string对象)
2)引用比较与值比较
引用比较
==
值比较
equals
String s1 = "Hello";
String s3 = "Hello";
String s2 = new String(“Hello");
String s4 = new String(“Hello");
System.out.println(s1==s2); // false
System.out.println(s1==s3); // true
System.out.println(s1.equals(s3)); // true
System.out.println(s2==s4); // false
System.out.println(s2.equals(s4)); // true
3)常用API:
charAt()
返回指定位置的字符
public class Test {
public static void main(String[] args) {
String a = "abcdef";
System.out.println(a.charAt(2)); // c
}
}
concat()
拼接字符串
public class Test {
public static void main(String[] args) {
String a = "abcdef";
String b = "123456";
System.out.println(a.concat(b)); // abcdef123456
}
}
indexOf() / lastIndexOf()
返回某个字符第一次/最后一次出现的下标,没有就返回-1
public class Test {
public static void main(String[] args) {
String a = "123456123456";
System.out.println(a.indexOf('2')); // 1
System.out.println(a.lastIndexOf('2')); // 7
}
}
toUpperCase() / toLowerCase()
字符串转换成大写/小写
public class Test {
public static void main(String[] args) {
String a = "abcdEfgh";
System.out.println(a.toUpperCase()); // ABCDEFGH
System.out.println(a.toLowerCase()); // abcdefgh
}
}
split()
切割字符串,返回字符串数组
public class Test {
public static void main(String[] args) {
String a = "123-456-789";
String[] b = a.split("-");
for (String x: b) {
System.out.println(x);
// 123
// 456
// 789
}
}
}
2.Stringbuffer
是一个具有对象引用传递特点的字符串对象。
1)Stringbuffer的特点:StringBuffer对象的值是可变的,对字符串的增加、插入、修改、删除等操作比String高效(不需多次创建新的对象)。
2)常用DPI:
append()
添加到字符串末尾
public class Test {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("123");
System.out.println(a.append('9')); // 1239
}
}
insert()
插入到字符串任意位置
public class Test {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("abcde");
StringBuffer b = new StringBuffer("123");
System.out.println(a.insert(2,b)); // ab123cde
}
}
reserve()
反转
public class Test {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("1234567");
System.out.println(a.reverse()); // 7654321
}
}
3.Stringbuild
JDK5 引入了StringBuilder,其与StringBuffer的 API兼容,性能比StringBuffer更高,但不是线程安全的。
1)常用DPI:
append()
添加到字符串末尾
public class Test {
public static void main(String[] args) {
StringBuilder a = new StringBuilder("123");
System.out.println(a.append('9')); // 1239
}
}
insert()
插入到字符串任意位置
public class Test {
public static void main(String[] args) {
StringBuilder a = new StringBuilder("abcde");
StringBuilder b = new StringBuilder("123");
System.out.println(a.insert(2,b)); // ab123cde
}
}
reserve()
反转
public class Test {
public static void main(String[] args) {
StringBuilder a = new StringBuilder("1234567");
System.out.println(a.reverse()); // 7654321
}
}
delete()
移除指定位置指定长度的字符
public class Test {
public static void main(String[] args) {
StringBuilder a = new StringBuilder("123qwert456");
System.out.println(a.delete(3,5)); // 123456
}
}
异同:
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且浪费大量优先的内存空间。
StringBuffer是可变的和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象。每个StringBuffer对象都有一定的缓冲区容量,当字符串大小没有超过容量时,不会分配新的容量,当字符串大小超过容量时,会自动增加容量 。
StringBuilder是可变的单线程操作字符串,速度更快,线程不安全。
应用场景:
当对字符串进行修改的时候,特别是字符串对象经常改变的情况下,需要使用 StringBuffer 和 StringBuilder 类。
StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类,但是在要求线程安全的情况下,则必须使用 StringBuffer 类。
为什么不建议在for循环中使用“+”进行字符串拼接
例: + 或者用 StringBuilder 进行字符串拼接
package com.wupx.demo;
/**
* @author wupx
* @date 2019/10/23
*/
public class StringConcatDemo {
public static void main(String[] args) {
long s1 = System.currentTimeMillis();
new StringConcatDemo().addMethod();
System.out.println("使用 + 拼接:" + (System.currentTimeMillis() - s1));
s1 = System.currentTimeMillis();
new StringConcatDemo().stringBuilderMethod();
System.out.println("使用 StringBuilder 拼接:" + (System.currentTimeMillis() - s1));
}
public String addMethod() {
String result = "";
for (int i = 0; i < 100000; i++) {
result += (i + "武培轩");
}
return result;
}
public String stringBuilderMethod() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < 100000; i++) {
result.append(i).append("武培轩");
}
return result.toString();
}
}
执行结果如下:
使用 + 拼接:29282
使用 StringBuilder 拼接:4