String
不可变的字符序列
StringBuffer
可变的字符序列JDK1.0
声明,线程安全 , 效率低
StringBuilder
可变的字符序列JDK5.0
声明 ,线程不安全 ,效率高
String s1 = new String() ; //char[] value = new char[0];
String s2 = new STring("abc"); //char[] value = new char[3]{'a','b,'c'};
char []value; //存储字符序列
int count; //实际存储的字符个数
StringBuilder sBuffer1 = new StringBuilder(); //char[] value = new char[16];
StringBuilder sBuffer2 = new StringBuilder("abc"); //char[] value = new char[16 + "abc".length];
sBuffer1.append("ab"); //value[0] = 'a'; value[1] = 'c';
sBuffer1.append("b"); //value[2] = 'b';
//最多修改16个
//一旦count超过value.length时,就需要扩容
//默认扩容为原有容量的2倍 + 2 。 并将原有的value数组中的元素复制到新的数组中
如果开发中,需要频繁的针对于字符串进行 增删改 等操作 ,建议使用StringBuffer
StringBuilder
替换String
若不涉及线程安全问题,建议使用StringBuffer
替换 StringBuilder
若大体确定需要操作的字符的个数,建议使用int capacity
的构造器,避免底层多次扩容操作,效率降低
StringBuilder、StringBuffer的API是完全一致的,并且很多方法与String相同。
//常用API
(1)StringBuffer append(xx):提供了很多的append()方法,用于进行字符串追加的方式拼接
(2)StringBuffer delete(int start, int end):删除[start,end)之间字符
(3)StringBuffer deleteCharAt(int index):删除[index]位置字符
(4)StringBuffer replace(int start, int end, String str):替换[start,end)范围的字符序列为str
(5)void setCharAt(int index, char c):替换[index]位置字符
(6)char charAt(int index):查找指定index位置上的字符
(7)StringBuffer insert(int index, xx):在[index]位置插入xx
(8)int length():返回存储的字符数据的长度
(9)StringBuffer reverse():反转
//其他API
(1)int indexOf(String str):在当前字符序列中查询str的第一次出现下标
(2)int indexOf(String str, int fromIndex):在当前字符序列[fromIndex,最后]中查询str的第一次出现下标
(3)int lastIndexOf(String str):在当前字符序列中查询str的最后一次出现下标
(4)int lastIndexOf(String str, int fromIndex):在当前字符序列[fromIndex,最后]中查询str的最后一次出现下标
(5)String substring(int start):截取当前字符序列[start,最后]
(6)String substring(int start, int end):截取当前字符序列[start,end)
(7)String toString():返回此序列中数据的字符串表示形式
(8)void setLength(int newLength) :设置当前字符序列长度为newLength
StringBuffer append(xx)
public class StringBufferTest{
public static void main(String []args){
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("abc").append("def"); //方法链调用
System.out.println(sBuilder); //abcdef
}
}
StringBuffer delete(int start, int end)
public class StringBufferTest{
public static void main(String []args){
StringBuilder stringBuilder = new StringBuilder("helloworld");
System.out.println(stringBuilder.delete(5, stringBuilder.length())); //hello
}
}
StringBuffer deleteCharAt(int index)
public calss StringBufferTest{
public static void main(String []args){
StringBuilder stringBuilder = new StringBuilder("Java");
System.out.println(stringBuilder.deleteCharAt(0)); //ava
}
}
StringBuffer replace(int start, int end, String str)
public class StringBufferTest{
public static void main(String []args){
StringBuilder stringBuilder = new StringBuilder("Java");
System.out.println(stringBuilder.replace(0, stringBuilder.length(), "helloworld")); //helloworld
System.out.println(stringBuilder); //helloworld
}
}
效率排序
StringBuilder
>StringBuffer
>String
public class RuntimeTest{
public static void main(String []args){
long startTime = 0L;
long endTime = 0L;
String text = "";
StringBuffer buffer = new StringBuffer("");
StringBuilder builder = new StringBuilder("");
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
buffer.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer运行时间" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder运行时间" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
text = text + i;
}
endTime = System.currentTimeMillis();
System.out.println("String运行时间" + (endTime - startTime));
}
}
//
//StringBuffer运行时间2
//StringBuilder运行时间1
//String运行时间274