What is StringBuilder?
Java StringBuilder is identical to StringBuffer except for one important difference it is not synchronized ,that means it is not thread safe.
We should prefer StringBuilder in all cases where you have only a single thread accessing your object.
StringBuilder Methods:
The important methods in StringBuilder Class are append() , insert() , replace() , delete() , reverse() , and capacity() .
How to use append()?
This method will concatenate the string representation of any type of data to the end of the invoking StringBuilder object.
Example:
public class Demo{ public static void main(String args[]){ StringBuilder sb=new StringBuilder("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[]){ StringBuilder sb=new StringBuilder("Fruitsgood"); sb.insert(6,"are"); System.out.println(sb); } }Try it Yourself
How to use reverse() ?
This method reverses the characters within a StringBuilder object.
Example:
public class Demo{ public static void main(String args[]){ StringBuilder sb=new StringBuilder("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[]){ StringBuilder str = new StringBuilder("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 StringBuilder object.
Example:
public class Demo{ public static void main(String args[]){ StringBuilder str = new StringBuilder(); System.out.println( str.capacity() ); } }Try it Yourself
How to use ensureCapacity() ?
This method is used to ensure minimum capacity of StringBuilder object.
Example:
public class Demo{ public static void main(String args[]){ StringBuilder str = new StringBuilder("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[]){ StringBuilder sb=new StringBuilder("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[]){ StringBuilder s1 = new StringBuilder("Hello"); StringBuilder 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[]){ StringBuilder c = new StringBuilder("Hello"); StringBuilder c1 = new StringBuilder(" World"); c.append(c1); System.out.println(c); } }
append() method of class StringBuilder is used to concatenate the string representation to the end of invoking string.
class output { public static void main(String args[]){ StringBuilder c = new StringBuilder("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