Java2all.com  

StringBuffer





Share This Topic -

StringBuffer class :

StringBuffer is a peer class of String. String creates strings of fixed length, while StringBuffer creates strings of flexible length that can be modified in terms of both length and content.

So Strings that need modification are handled by StringBuffer class.

We can insert characters and substrings in the middle of a string, or append another string to the end.

            StringBufer defines these Constructor:

            StringBuffer()

            StringBuffer(int size)

            StringBuffer(String str)

Method Call

Task Performed

Sb.length()

Gives the current length of a StringBuffer.

Sb.capacity()

Gives the total allocated capacity (default 16)

setLength(int len)

Set the length of the buffer within a String Buffer object.

charAt(int where)

Gives the value of character

setCharAt(int where, char ch)

Set the value of a character within a StringBuffer.

S1.append(s2)

Appends the string s2 to s1 at the end

S1.insert(n,s2)

Inserts the string s2 at the position n of the string s1

S1.reverse()

Reverse the string of s1

S1.deleteCharAt(nth)

Delete the nth character of string s1

S1.delete(StartIndex, endIndex)

Delete characters from start to end.

EX :

 import java.util.*;

class strbuf1
{
		public static void main(String args[])
		{
			StringBuffer s1 = new StringBuffer();
			StringBuffer s2 = new StringBuffer("Bhagirath");
			StringBuffer s3 = new StringBuffer(s2);
			StringBuffer s4 = new StringBuffer(100);
			System.out.println("s1 = " + s1);
			System.out.println("s2 = " + s2);
			System.out.println("s3 = " + s3);
			
			System.out.println("s1.length = " + s1.length());
			System.out.println("s2.length = " + s2.length());
			System.out.println("s3.length = " + s3.length());
			System.out.println("s4.length = " + s4.length());
			
			System.out.println("s1.capacity = " + s1.capacity());
			System.out.println("s2.capacity = " + s2.capacity());
			System.out.println("s3.capacity = " + s3.capacity());
			System.out.println("s4.capacity = " + s4.capacity());
			
			
		}
}

 
Output :
 
s1 = 
s2 = Bhagirath
s3 = Bhagirath
s1.length = 0
s2.length = 9
s3.length = 9
s4.length = 0
s1.capacity = 16
s2.capacity = 25
s3.capacity = 25
s4.capacity = 100

EX :

 import java.util.*;
class strbuf2
{
		public static void main(String args[])
		{
			StringBuffer s1 = new StringBuffer("Java2all");
			StringBuffer s2 = new StringBuffer("Hello");
			System.out.println("s1 = " + s1);
			//System.out.println("s1.charAt(5) = " + s1.charAt(5));
			//s1.setCharAt(5,'z');
			//System.out.println("s1 = " + s1); 
			//System.out.println("Inserting String = " + s1.insert(5,s2));
			//System.out.println("s1 = " + s1); 
			//System.out.println("Appending String = " + s1.append(s2));
			//System.out.println("s1 = " + s1); 
			//System.out.println("Reversing String = " + s1.reverse()); 
			//System.out.println("Deleting 5th character = " + s1.deleteCharAt(5)); 
			System.out.println("Deleting 5 to 8 character = " + s1.delete(5,8)); 
		}
}

 
Output :
 
s1 = Java2all
Deleting 5 to 8 character = Java2

EX :

 import java.util.*;

public class strbuf3
{
    public static void main(String[] args)
    {
 
        StringBuffer s = new StringBuffer("Hello world!");

        System.out.println("s = " + s);
        System.out.println("s.length() = " + s.length());
        System.out.println("s.length() = " + s.capacity());
        // Change the length of buffer to 5 characters:
        s.setLength(5);

        System.out.println(s);
        System.out.println("s.length() = " + s.length());
        System.out.println("s.length() = " + s.capacity());
        
    }
}

 
Output :
 
s = Hello world!
s.length() = 12
s.length() = 28
Hello
s.length() = 5
s.length() = 28