Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Java gives you all sorts of features when it comes to programming. This article will precisely focus on one such topic that is StringBuilder In Java. Following are pointers discussed in this article,
Strings in java are a sequence of immutable characters. StringBuffer, on the other hand, is used to create a sequence of mutable characters. StringBuilder is similar to the StringBuffer class, but it does not provide any guarantee of synchronization, whatsoever. However, it is much faster in nature under most implementations. It must be noted that , StringBuilder class is not safe for use by multiple threads.
Let us continue with this article and take a look at different methods
StringBuilder Methods
The following methods are used in the StringBuilder class:
This method adds the specified elements to the existing string.
class Main{ public static void main(String args[]){ StringBuilder str=new StringBuilder("Rock"); str.append("Roll"); System.out.println(str); } }
Output:
RockRoll
This method inserts a string at the specified index.
public class Main{ public static void main(String args[]){ StringBuilder str=new StringBuilder("Rock "); str.insert(2,"Roll"); System.out.println(str); } }
Output:
RoRollck
This method replaces the string from the specified beginIndex to the endIndex.
public class Main{ public static void main(String args[]){ StringBuilder str=new StringBuilder("Rock"); str.replace(1,3,"Roll"); System.out.println(str); } }
Output:
RRollk
This method deletes the specific string from the beginIndex to the endIndex.
public class Main{ public static void main(String args[]){ StringBuilder str=new StringBuilder("Rock"); str.delete(1,3); System.out.println(str); } }
Output:
Rk
This method reverses the string present in the StringBuilder.
public class Main{ public static void main(String args[]){ StringBuilder str=new StringBuilder("Rock"); str.reverse(); System.out.println(str); } }
Output:
kcoR
The current capacity of the Builder can be determined by this method. It must be noted that the default capacity of the Builder is 16. The capacity of the builder can be increased by : (oldcapacity*2)+2.
public class Main{ public static void main(String args[]){ StringBuilder str=new StringBuilder(); System.out.println(str.capacity()); str.append("Rock"); System.out.println(str.capacity()); str.append("It is a good day to rock"); System.out.println(str.capacity()); } }
Output:
16
16
34
Thus we have come to an end of this article on ‘StringBuilder In Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co