មេរៀនទី៤១: StringBuilder class

StringBuilder class ត្រូវបានប្រើដើម្បីបង្កើត create mutable (modifiable) string។ StringBuilder class គឺជាឈ្មោះដូច StringBuffer class ដែលវាមិនមែនជា non-synchronized។ វាគឺជាអថេរ ប្រើជាមួយ JDK1.5។
ការប្រើរួម Constructors នៃ StringBuilder class:
1.    StringBuilder(): creates an empty string Builder with the initial capacity of 16.
2.    StringBuilder(String str): creates a string Builder with the specified string.
3.    StringBuilder(int length): creates an empty string Builder with the specified capacity as length.
ការប្រើរួម methods នៃ StringBuilder class:
1.    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.
2.    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.
3.    public StringBuilder replace(int startIndex, int endIndex, String str): is used to replace the string from specified startIndex and endIndex.
4.    public StringBuilder delete(int startIndex, int endIndex): is used to delete the string from specified startIndex and endIndex.
5.    public StringBuilder reverse(): is used to reverse the string.
6.    public int capacity(): is used to return the current capacity.
7.    public void ensureCapacity(int minimumCapacity): is used to ensure the capacity at least equal to the given minimum.
8.    public char charAt(int index): is used to return the character at the specified position.
9.    public int length(): is used to return the length of the string i.e. total number of characters.
10.    public String substring(int beginIndex): is used to return the substring from the specified beginIndex.
11.    public String substring(int beginIndex, int endIndex): is used to return the substring from the specified beginIndex and endIndex.
ឧទាហរណ៍នៃ StringBuilder class by append() method
The append() method concatenates the given argument with this string.
class A{
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
}
}
ឧទាហរណ៍នៃ insert() method of StringBuilder class
The insert() method inserts the given string with this string at the given position.
class A{
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
}
}
ឧទាហរណ៍នៃ replace() method of StringBuilder class
The replace() method replaces the given string from the specified beginIndex and endIndex.
class A{
public static void main(String args[]){
StringBuilder sb=new StringBuilder(“Hello”);
sb.replace(1,3,”Java”);
System.out.println(sb);//prints HJavalo
}
}
ឧទាហរណ៍នៃ delete() method of StringBuilder class
The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.
class A{
public static void main(String args[]){
StringBuilder sb=new StringBuilder(“Hello”);
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
ឧទាហរណ៍នៃ reverse() method of StringBuilder class
The reverse() method of StringBuilder class reverses the current string.
class A{
public static void main(String args[]){
StringBuilder sb=new StringBuilder(“Hello”);
sb.reverse();
System.out.println(sb);//prints olleH
}
}
ឧទាហរណ៍នៃ capacity() method of StringBuilder class
The capacity() method of StringBuilder class returns the current capacity of the Builder. The default capacity of the Builder is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
class A{
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
}
}
ឧទាហរណ៍នៃ ensureCapacity() method of StringBuilder class
The ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
class A{
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
}
}