Java Program to Clear the StringBuffer
What are Strings?
A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;
In this Article, we will write a program to check if two strings are anagram.
Strings:
We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.
What is StringBuffer?
In Java, a StringBuffer is a class that provides a mutable sequence of characters. It is similar to a String, but can be modified after it is created. StringBuffer is thread-safe, meaning that multiple threads can access a single StringBuffer instance without causing any problems. This makes it a useful choice for use in a multithreaded environment. Some common methods of StringBuffer include append(), insert(), replace(), and delete().
There are Several ways to delete the content or clear a stringbuffer in java, some of the widely and common used ways in java are :
- By changing its length to 0.
- By creating a new instance of the StringBuffer.
- By using delete Function
- By using replace Function
Program to clear StringBuffer by Changing the length to 0
import java.util.*; public class Main{ public static void main(String[] args) { // creating a string buffer StringBuffer Prep = new StringBuffer(); // add string to string buffer Prep.append("Lakshit"); Prep.append(" Mittal"); System.out.println("Before Clearing: " + Prep); // clearing the stringbuffer using setLength() Prep.setLength(0); System.out.println("After Clearing: " + Prep); } }
Output: Before Clearing: Lakshit Mittal After Clearing:
Program to clear StringBuffer by Creating a new Instance :
import java.util.*; public class Main{ public static void main(String[] args) { // creating a string buffer StringBuffer Prep = new StringBuffer(); // add string to string buffer Prep.append("Lakshit"); Prep.append(" Mittal"); System.out.println("Before Clearing: " + Prep); // clearing the stringbuffer using new instance Prep = new StringBuffer(); System.out.println("After Clearing: " + Prep); } }
Output: Before Clearing: Lakshit Mittal After Clearing:
Program to clear StringBuffer by using Delete Function :
import java.util.*; public class Main{ public static void main(String[] args) { // creating a string buffer StringBuffer Prep = new StringBuffer(); // add string to string buffer Prep.append("Lakshit"); Prep.append(" Mittal"); System.out.println("Before Clearing: " + Prep); // clearing the stringbuffer using new instance Prep.delete(0, Prep.length()); System.out.println("After Clearing: " + Prep); } }
Output: Before Clearing: Lakshit Mittal After Clearing:
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment