Stringbuffer in Java
April 30th, 2019
Java String Buffer
Definition for String Buffer
Java String Buffer is similar to the String value class save that String function class is immutable, whereas the Java String-Buffer class remains mutable. Later than the java String-Buffer objects value can be converted back into the String objects.
How to create String-Buffer Objects
Java String-Buffer can be defined as easily by the use of the new operator and bypassing the string value inside the String-Buffer constructor.
Syntax
StringBuffer <object> = new StringBuffer(String_value) |
Example
public class Test {
public static void main(String args[]) {
StringBuffer sBuffer = new StringBuffer("HelloWorld");
sBuffer.append(" String Buffer"); System.out.println(sBuffer); } }
|
Out Put
How to Data Value insert and appending the Java String-Buffer
Below method you having using data inserting/appending a string, an empty Java String-Buffer object is created.
Syntax
StringBuffer insert(int a, b);
// inserts b in the position a
Syntax for append(): StringBuffer append (b1);
// appends b1 to the StringBuffer
|
Example Code insert
public class Test { public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hellototheworld");
sb.insert(5, "Welcome");
System.out.println(sb);
}
}
|
OutPut
Example code for Append
public class Test { public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Welcome");
sb.append(" String Buffer"); System.out.println(sb);
}
}
|
Output
Deleting Creating of Java Sting-Buffer
Delete the characters starting from the position p1 to p2-1th position.
Syntax:
StringBuffer delete(int p1, int p2) |
Example Code
import java.lang.*;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("Java long package");
System.out.println("buffer = " + buff);
buff.delete(4, 9);
System.out.println("After deletion = " + buff);
}
}
|
Output
buffer : Java long package
after deletion : Java package
|
How to reverse the String Value using StringBuffer
Reversing a string is as simple as using the reverse() method with the StringBuffer object.
Syntax
Example Code
public class Test {
public static void main(String args[]) {
StringBuffer buffer = new StringBuffer("GangBoard");
buffer.reverse();
System.out.println(buffer);
}
}
|
Output
Finding the length of the string value using StringBuffer
Finding the length of the StringBuffer is simple.
Syntax:
Example Code
import java.lang.*;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("GangBoard");
System.out.println("length = " + buff.length());
buff = new StringBuffer("");
System.out.println("length = " + buff.length());
}
|
OutPut
Replacing character of Java String-Buffer
Sequence of Characters can be replaced with a new set of characters using the re-place() method.
Syntax:
StringBuffer replace(int i, int j, String s) |
Example Code
import java.lang.*;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("Java long Package");
System.out.println("buffer = " + buff);
// prints the stringbuffer after replacing
System.out.println("After replacing: " + buff);
}
}
|
Output
buffer = Java long Package
After replacing: Java Lang Package |
Finding the substring using String-Buffer
Retrieves the characters starting from the position I from the StringBuffer object.
Syntax
Stringbuffer substring(int i); |
Example Code
import java.lang.*;
public class StringBufferDemo {
public static void main(String[] args)
{ StringBuffer buff = new StringBuffer("GangBoard");
System.out.println("buffer = " + buff);
// prints substring from index 3 System.out.println("substring is = " + buff.substring(3));
/* prints substring from index 1 to 4 excluding character at 4th index */ System.out.println("substring is = " + buff.substring(1, 4)); } }
|
Out Put
buffer = GangBoardSubstring is = nt
substring is = esa
|
Indeax of a Character in Java String-Buffer
Position of a character(s) can be obtained relating indexOf() method. Returns this first occurrence from the substring of left to right.
Syntax
Example Code
import java.lang.*; public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("Program code");
System.out.println("buffer = " + buff);
System.out.println("Index of substring = " + buff.indexOf("age")); System.out.println("Index of substring = " + buff.indexOf("k")); } }
|
Output
buffer = Program code
Index of substring = 10
Index of substring = -1 |