📜  Java StringBuffer类

📅  最后修改于: 2020-09-25 14:54:55             🧑  作者: Mango

Java StringBuffer类

JavaStringBuffer类用于创建可变(可修改)字符串。Java中的StringBuffer类与String类相同,但它是可变的,即可以更改。

注意:Java StringBuffer类是线程安全的,即多个线程无法同时访问它。因此,这是安全的,将导致订单。

StringBuffer类的重要构造函数

Constructor Description
StringBuffer() creates an empty string buffer with the initial capacity of 16.
StringBuffer(String str) creates a string buffer with the specified string.
StringBuffer(int capacity) creates an empty string buffer with the specified capacity as length.

StringBuffer类的重要方法

Modifier and Type Method Description
public synchronized StringBuffer append(String s) is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.
public synchronized StringBuffer insert(int offset, String s) is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.
public synchronized StringBuffer replace(int startIndex, int endIndex, String str) is used to replace the string from specified startIndex and endIndex.
public synchronized StringBuffer delete(int startIndex, int endIndex) is used to delete the string from specified startIndex and endIndex.
public synchronized StringBuffer reverse() is used to reverse the string.
public int capacity() is used to return the current capacity.
public void ensureCapacity(int minimumCapacity) is used to ensure the capacity at least equal to the given minimum.
public char charAt(int index) is used to return the character at the specified position.
public int length() is used to return the length of the string i.e. total number of characters.
public String substring(int beginIndex) is used to return the substring from the specified beginIndex.
public String substring(int beginIndex, int endIndex) is used to return the substring from the specified beginIndex and endIndex.

什么是可变字符串

可以修改或改变的字符串被称为可变的字符串。StringBuffer和StringBuilder类用于创建可变字符串。

1)StringBuffer append()方法

append()方法将给定参数与此字符串。

class StringBufferExample{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello ");  
sb.append("Java");//now original string is changed  
System.out.println(sb);//prints Hello Java  
}  
}  

2)StringBuffer insert()方法

insert()方法将给定字符串与该字符串插入给定位置。

class StringBufferExample2{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello ");  
sb.insert(1,"Java");//now original string is changed  
System.out.println(sb);//prints HJavaello  
}  
}  

3)StringBuffer replace()方法

replace()方法从指定的beginIndex和endIndex替换给定的字符串。

class StringBufferExample3{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.replace(1,3,"Java");  
System.out.println(sb);//prints HJavalo  
}  
}  

4)StringBuffer delete()方法

StringBuffer类的delete()方法将字符串从指定的beginIndex删除到endIndex。

class StringBufferExample4{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.delete(1,3);  
System.out.println(sb);//prints Hlo  
}  
}  

5)StringBuffer reverse()方法

StringBuilder类的reverse()方法将当前字符串反转。

class StringBufferExample5{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.reverse();  
System.out.println(sb);//prints olleH  
}  
}  

6)StringBuffer Capacity()方法

StringBuffer类的Capacity()方法返回缓冲区的当前容量。缓冲区的默认容量为16。如果字符数从当前容量增加,它将增加(oldcapacity*2)+2。例如,如果您当前的容量为16,则将为(16*2)+2=34。

class StringBufferExample6{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer();  
System.out.println(sb.capacity());//default 16  
sb.append("Hello");  
System.out.println(sb.capacity());//now 16  
sb.append("java is my favourite language");  
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
}  
}  

7)StringBuffer sureCapacity()方法

StringBuffer类的ensureCapacity()方法可确保给定容量为当前容量的最小值。如果大于当前容量,则将容量增加(oldcapacity*2)+2。例如,如果您当前的容量为16,则将为(16*2)+2=34。

class StringBufferExample7{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer();  
System.out.println(sb.capacity());//default 16  
sb.append("Hello");  
System.out.println(sb.capacity());//now 16  
sb.append("java is my favourite language");  
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
sb.ensureCapacity(10);//now no change  
System.out.println(sb.capacity());//now 34  
sb.ensureCapacity(50);//now (34*2)+2  
System.out.println(sb.capacity());//now 70  
}  
}