StringBuffer class and it's methods


StringBuffer class in java :


StringBuffer supports a modifiable string.
StringBuffer class is used to create mutable string.


--->StringBuffer represents growable and writable character sequences.


--->StringBuffer class is thread safe.i,e multiple threads can't access at same time.


--->StringBuffer creates strings of flexible length that can be modified in terms of both length and content.


--->StringBuffer is a peer class of String.


--->StringBuffer may have characters and substrings inserted in the middle or appended to the end.


--->StringBuffer will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth.


--->It is available in java.lang package.


StringBuffer Constructors :


StringBuffer defines these four constructors:


1.StringBuffer( ) :
The default constructor (the one with no parameters) reserves room for 16 characters without reallocation.


2.StringBuffer(int size) :
It accepts an integer argument that explicitly sets the size of the buffer.


3. StringBuffer(String str) :
It accepts a String argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation.
StringBuffer allocates room for 16 additional characters when no specific
buffer length is requested, because reallocation is a costly process in terms of time. Also, frequent reallocations can fragment memory. By allocating room for a few extra characters,
StringBuffer reduces the number of reallocations that take place.


4. StringBuffer(CharSequence chars) :
This constructor creates an object that contains the character sequence contained in chars and reserves room for 16 more characters


StringBuffer class methods :

StringBuffer class defines a number of methods that allow us to accomplish a variety of string manipulation task.


1. length():
it is used to get the number of charecters of any string.
Or
It is used to return the length of given string in integer.


Example :
public class stringBufferDemo
{
public static void main(String args[])
{
StringBuffer s=new StringBuffer("Java");
int l=s.length();
System.out.println("Length="+l);
}
}


Output:
Length=4


2. charAt(index):
This method is used to get the character at a given index value.
Or
This will obtain a character from specified position.


Example :
public class stringBufferDemo
{
public static void main(String args[])
{
StringBuffer s = new StringBuffer("Java");
char c=s.charAt(3);
System.out.println("character is="+c);
}
}


Output:
character is=a


3. indexOf(string):
This method is used to find the index value of given string.
It always gives the starting index value of first occurance of string.


Example :
public class stringBufferDemo
{
public static void main(String args[])
{
StringBuffer  s=new StringBuffer("Java is programming language");
int i=s.indexOf("programming");
System.out.println("index value is="+i);
}
}


Output :
Index value is=8


4. capacity( ) :
Total allocated capacity can be found through the capacity( ) method.
It returns the current capacity of the buffer.
The default capacity of the buffer is 16.
If the number of charecters increases from its current capacity it increases the capacity by ((oldcapacity *n)+n)

int capacity( )


Example:
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
System.out.println("capacity = " + sb.capacity());
}
}


Output:
capacity = 21
Its capacity is 21 because room for 16 additional characters is automatically added.


5. append():

This method is used to add the new string at the end of original string.


Example :
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("java is easy");
System.out.println(sb.append("to learn"));

}
}


Output :
java is easy to learn


6. reverse() :
This method is used to reverse the given string and also the new value replaces by old string.


Example :
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("java");
System.out.println(sb.reverse());

}
}


Output :
avaj


7. insert() :
This method is used to insert either string or character or integer or real constants or boolean value at the specified index value of given string.


Example :
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("This is my java code");
System.out.println(sb.insert(11,"first"));

}
}


Output :
This is my first java code


8. delete() : This method is used to delete string from given string based on index value.


Example :
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("java is easy to learn");
StringBuffer s=sb.delete(8,13);
System.out.println(s);

}
}


Output :
java is  to learn


9. deleteCharAt() : This method is used to delete a character from given string based on index value.


Example :
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("java");

System.out.println(sb.deleteCharAt(3));

}
}


Output :
jav


10. toString() :
This method is used to convert mutable string value to immutable string.


Example :
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("java");
String s =sb.toString();
System.out.println(s);

}
}


Output :
java


11. replace() :
This method is used to replace any old string with new string based on index value.


Example:

class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("This is my code");
System.out.println(sb.replace(8,10,"java "));

}
}


Output :
This is java code


Note: java string is replaced with old string (my) which is available between 8 to 10 index value.


12. substring():
This method is used to get the part of given string.
It is used to extract a substring from given string.

substring(n):it gives substring from nth character.


Example :
public class stringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Java is programming language");

System.out.println(s.substring(8));
}
}


Output :
programming language


Or
Substring(n,m) :
It gives substring starting from nth character upto mth(not including mth).


Example :
public class stringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Java is programming language");

System.out.println(s.substring(8,15));
}
}


Output:
program

Note:substring() method : s is  in small letter where as string class  s is in capital letter(subString() ).


Write a java program to demonstrate StringBuffer class.

Or
write a java program to implement methods of StringBuffer class.


Program:
public class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Welcome");
System.out.println(sb);

System.out.println(sb.append("to java"));

System.out.println(sb.insert(4,"How"));

System.out.println(sb.delete(4,7));

System.out.println("reverse="+sb.reverse());

System.out.println("length="+sb.length());

System.out.println(sb.reverse());

System.out.println("index value is ="+sb.indexOf("Welcome"));

System.out.println("character is="+sb.CharAt(6));

System.out.println("substring is="+sb.substring(8));

System.out.println("subsequence="+sb.subsequence(11,15));
}
}



Output:
Welcome
Welcome to java
WelcHowome to java
Welcome to java
reverse =avaj ot emoclew
length=15
Welcome to java
index value is=0
chararecter is=e
substring is=to java
subsequence=java

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java