Java StringBuffer Class
In this instructional exercise, you will find out about Java StringBuffer class and its significant methods with examples.
StringBuffer in Java is utilized to make changeable strings. Here variable methods, we can adjust or change the strings.
StringBuffer class is string safe which implies different strings can’t get to a similar object at the same time.
Aside from StringBuffer, we can likewise make changeable strings utilizing StringBuilder class. We will examine it in up and coming instructional exercises.
Java StringBuffer Constructors
StringBuffer class in Java gives the following four constructors.
StringBuffer() – Creates an unfilled string with the default limit of 16 characters.
StringBuffer(int size) – Creates an unfilled string with the predefined limit.
StringBuffer(String str) – Creates a string with an underlying worth contained by str. The absolute limit of the made string is the aggregate of the length of beginning worth and 16 additional characters.
StringBuffer(CharSequence singes) – Creates a string with an underlying worth contained by roasts. It additionally saves additional room for 16 additional characters.
Note: When we do alteration in a given string and in the event that the length of new string surpasses the present limit, then the limit is expanded by (oldcapacity*2)+2.
Beneath I have examined hardly any significant methods of StringBuffer class which are not the same as methods of String in Java.
Java StringBuffer Methods
StringBuffer length() and limit() Methods
The length of string esteem present in StringBuffer object is determined by length() method.
The all-out limit of an object is determined by the limit() method. See underneath example code to comprehend it more clearly.
int length()
int capacity()
Example
Java StringBuffer Class
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("Hello");
System.out.println("length = "+str.length());
System.out.println("capacity = "+str.capacity());
}
}
In the above example, str.length() gives yield 5 since the length of Hello is 5. While str.capacity() gives
yield 21 since space for 16 additional characters is naturally included.
StringBuffer ensureCapacity() Method
This method guarantees that the given limit is the base of the present limit. In the event that the given limit is
more than the present limit, then the present limit is expanded by (oldcapacity2)+2. For example on the off chance that the present limit is 20, then it will become (202)+2 = 42.
Syntax:
void ensureCapacity(int minCapacity)
Example:
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("Hello");
System.out.println("capacity = "+str.capacity());
str.ensureCapacity(30);
System.out.println("capacity = "+str.capacity());
}
}
Output:
StringBuffer setLength() Method
As its name appears, it is utilized to set the length of string inside an object of StringBuffer class.
In the event that the new length is not exactly the present length, then the characters past the new length are lost.
Syntax:
void setLength(int len)
StringBuffer charAt() and setCharAt() Methods
The charAt() method is utilized to get to a specific character in a given string. It is same as we use for String class object.
We can supplant a specific character in a string with another character utilizing setCharAt() method.
Syntax:
char charAt(int where)
void setCharAt(int where, char ch)
Example:
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("Hello");
System.out.println("before = "+str);
str.setCharAt(1,'a');
System.out.println("after = "+str);
}
}
StringBuffer affix() Method
The affix() method is utilized to include a string toward the finish of a given string. It has a few over-burden forms as given underneath.
Syntax:
StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)
Example:
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("Hello");
System.out.println("before = "+str);
str.append("World");
System.out.println("after = "+str);
}
}
Output:
StringBuffer embed() Method
The addition() method is utilized to embed a string in a given string.
Same as like annex() method it likewise has different over-burden forms. We need to determine the record
where we need to embed and the string to be embedded as contentions.
Syntax:
StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)
Example:
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("TheProgrammer");
System.out.println("before = "+str);
str.insert(3,"Crazy");
System.out.println("after = "+str);
}
}
StringBuffer reverse() Method
The characters inside a StringBuffer object can be turned around utilizing the reverse() method.
Syntax:
StringBuffer reverse()
Example:
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("JustTechReview");
System.out.println("before = "+str);
str.reverse();
System.out.println("after = "+str);
}
}
Output:
StringBuffer replace() Method
The replace() method is utilized to replace a piece of a given string with another string.
We need to simply indicate the startindex, endindex and the string with which we need to replace as contentions.
Note: Here the end file is one not as much as what we have determined in the contention, for example, endIndex – 1.
Syntax:
StringBuffer replace(int startIndex, int endIndex, String str)
Example:
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("JustTechReview");
System.out.println("before = "+str);
str.replace(8,18,"Coder");
System.out.println("after = "+str);
}
}
Output:
StringBuffer erase() and deleteCharAt() Method
The erase() method is utilized to erase a succession of characters from beginning to end the record. The deleteCharAt() method is utilized to erase single character at an explicit list.
Note: Here the end file is one not as much as what we have determined in the contention, for example, endIndex – 1.
Syntax:
StringBuffer delete(int startIndex, int endIndex)
StringBuffer deleteCharAt(int where)
Example:
class StringBufferExample
{
public static void main(String...s)
{
StringBuffer str=new StringBuffer("HelloWorld");
System.out.println("before = "+str);
str.delete(0,2);
System.out.println("delete(0,2) = "+str);
str.deleteCharAt(1);
System.out.println("deleteCharAt(1) = "+str);
}
}
There are such a significant number of other methods of Java StringBuffer class.
On the off chance that you discovered anything mistaken or have any questions with respect to the above instructional
exercise, then please remark beneath.