📜  StringBuilder类

📅  最后修改于: 2020-09-24 05:33:07             🧑  作者: Mango

Java StringBuilder类

JavaStringBuilder类用于创建可变(可修改)字符串。JavaStringBuilder类与StringBuffer类相同,但它是不同步的。从JDK1.5开始可用。

StringBuilder类的重要构造函数

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

StringBuilder类的重要方法

Method Description
public StringBuilder 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 StringBuilder 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 StringBuilder replace(int startIndex, int endIndex, String str) is used to replace the string from specified startIndex and endIndex.
public StringBuilder delete(int startIndex, int endIndex) is used to delete the string from specified startIndex and endIndex.
public StringBuilder 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.

Java StringBuilder示例

让我们看一下StringBuilder类的不同方法的示例。

1)StringBuilder append()方法

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

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

2)StringBuilder insert()方法

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

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

3)StringBuilder replace()方法

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

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

4)StringBuilder delete()方法

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

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

5)StringBuilder reverse()方法

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

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

6)StringBuilder Capacity()方法

StringBuilder类的Capacity()方法返回Builder的当前容量。生成器的默认容量为16。如果字符数从其当前容量增加,它将增加(oldcapacity*2)+2。例如,如果您当前的容量为16,则将为(16*2)+2=34。

"class StringBuilderExample6{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder();  
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)StringBuilder sureCapacity()方法

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

"class StringBuilderExample7{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder();  
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  
}  
}