What is StringBuffer?
The StringBuffer class is used to represent characters that can be modified.
StringBuffer is mainly used for the dynamic string concatenation which enhances the performance.
A string buffer implements a mutable sequence of characters.
A string buffer is like a String, but can be modified.
When StringBuffer is used?
StringBuffer class is used when we have to make lot of modifications to our string.
It is also thread safe i.e multiple threads cannot access it simultaneously.
Methods of StringBuffer:
How to use append()?
This method will concatenate the string representation of any type of data to the end of the invoking StringBuffer object.
Example:
public class Demo{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Fruits"); sb.append("are good for health"); sb.append(123); System.out.println(sb); } }Try it Yourself
How to use insert() ?
This method inserts one string into another.
Example:
public class Demo{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Fruitsgood"); sb.insert(6,"are"); System.out.println(sb); } }Try it Yourself
How to use reverse() ?
This method reverses the characters within a StringBuffer object.
Example:
public class Demo{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("fruits"); sb.insert(6,"are"); System.out.println("reverse:"+sb.reverse()); } }Try it Yourself
How to use replace() ?
This method replaces the string from specified start index to the end index.
Example:
public class Demo{ public static void main(String args[]){ StringBuffer str = new StringBuffer("Hello World"); str.replace( 6, 11, "java"); System.out.println(str); } }Try it Yourself
How to use capacity() ?
This method returns the current capacity of StringBuffer object.
Example:
public class Demo{ public static void main(String args[]){ StringBuffer str = new StringBuffer(); System.out.println( str.capacity() ); } }Try it Yourself
How to use ensureCapacity() ?
This method is used to ensure minimum capacity of StringBuffer object.
Example:
public class Demo{ public static void main(String args[]){ StringBuffer str = new StringBuffer("hello"); str.ensureCapacity(10); } }Try it Yourself
How to use delete() ?
This method delete data from string.
Example:
public class Demo{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Fruits are very good"); sb.delete(10,15); System.out.println("After delete:"+sb); } }Try it Yourself
Solve this!!
class output { public static void main(String args[]){ StringBuffer s1 = new StringBuffer("Hello"); StringBuffer s2 = s1.reverse(); System.out.println(s2); } }
reverse() method reverses all characters. It returns the reversed object on which it was called.
class output { public static void main(String args[]) { StringBuffer c = new StringBuffer("Hello"); StringBuffer c1 = new StringBuffer(" World"); c.append(c1); System.out.println(c); } }
append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
class output { public static void main(String args[]){ StringBuffer c = new StringBuffer("Hello"); c.delete(0,2); System.out.println(c); } }
delete(0,2) is used to delete the characters from 0 th position to 1 st position.
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling