Showing posts with label StringBuffer class in JAVA. Show all posts
Showing posts with label StringBuffer class in JAVA. Show all posts

Jul 21, 2020

StringBuffer CLASS in JAVA | Java Language | Coding Winds

StringBuffer CLASS

 Hey guys we are back with another blog and in this blog we are gonna talk about StringBuffer class. So you might have been acknowledged with what Strings are from the earlier blogs, but Strings with StringBuffer class gives us more methods to manipulate the strings and with lesser limitations, that is one thing. Second, Strings declared using string buffer constructors are dynamic, i.e. one can increase or decrease their capacity anytime in the code.

So let’s see the syntax of the how to declare a StringBuffer constructor in Java:

StringBuffer str = new StringBuffer(int Size);

Now note that if the capacity of the string is not mentioned inside the parentheses (Ex: StringBuffer str = new StringBuffer(); ) the compiler by default we allocate the capacity for 16 characters to that the string, but obviously that’d be extendable. Now let’s have a look at another way in which we can initialise the String with the constructor.

StringBuffer str = new StringBuffer(“Hello Readers”);

Note that when the String value is declared right away with the constructor the capacity of the string increases to 16 + (the current length of the string).

Now let us understand the methods that we have for the StringBuffer class.

i) length() & capacity() : So by the name you might have guessed it right that what does these methods do. ‘.length()’ tells us the current length of the String and (.capacity()) tell us the capacity of the String.

Ex:

public class StringBuffer_class  {

     public static void main(String[] args) {

         StringBuffer STR = new StringBuffer();

         StringBuffer STR2 = new StringBuffer("Coding Winds");

        

         System.out.println(STR);

         System.out.println("length of STR :"+STR.length()+"\ncapacity of STR:"+STR.capacity());

        

         System.out.println("\n"+STR2);

         System.out.println("length of STR2 :"+STR2.length()+"\ncapacity of STR2:"+STR2.capacity());

     }

}

    

 

OUTPUT:

 

length of STR :0

capacity of STR:16

 

Coding Winds

length of STR2 :12

capacity of STR2:28

 

So one can see above, that when the String has no value (or when its length is 0) and is declared without the size, then the capacity of the string (pre allocated ) is 16, but when it has some value the capacity is increased to 16+(length of the string) , i.e., 28 for the above case.

ii)append() : This method can be used to add (or append) something at the end of a String.

Ex:

public class StringBuffer_class {

     public static void main(String[] args) {

         StringBuffer STR = new StringBuffer("Coding");

        

         STR.append(" Winds");

         System.out.println(STR);

     }

}   

 

OUTPUT:

Coding Winds

 

 

iii)insert() : This method is used to insert a string or number at an specific index ( .insert(int index, “String”); )

Ex:

 

public class StringBuffer_class {

     public static void main(String[] args) {

         StringBuffer STR = new StringBuffer("Hello");

        

         STR.append(" People!");

         STR.insert(13," What's up?");

         STR.insert(24, 10); /*integers can be written without double quotes */

         System.out.println(STR);

     }

}

    

OUTPUT:

Hello People! What's up?10

 

iv)reverse() : As the method name suggests itself, it is used to reverse a String

Ex:

public class StringBuffer_class {

     public static void main(String[] args) {

         StringBuffer STR = new StringBuffer("YENSID");

        

      STR.reverse();

         System.out.println(STR);

     }

}

    

 

OUTPUT:

DISNEY

 

 

    

v) .delete() & .deleteCharAt() : These two methods are used to delete the characters of a string of some indexes and at a particular index , respectively.

Ex:

 

public class StringBuffer_class {

     public static void main(String[] args) {

         StringBuffer STR = new StringBuffer("YENSID");

        

      STR.delete(0,3);

         System.out.println(STR);

        

         StringBuffer STR2 = new StringBuffer("Hello 2 All");

      STR2.deleteCharAt(6);

      System.out.println(STR2);

     }

}

    

 

OUTPUT:

SID

Hello  All

 

vi) .replace(): This method is used to replace a certain part of the string with a new one.

 

Syntax:

 stringName.replace(int startIndex, int endIndex, “new string”);

 

Ex:

public class StringBuffer_class {

     public static void main(String[] args) {

        

         StringBuffer STR = new StringBuffer("Hello 2 All");

         STR.replace(6,7,"to");

        System.out.println(STR);

    

     }

}

    

 

OUTPUT:

Hello to All

 

vii) setCharAt(): This method sets a new char at the specified index:

public class StringBuffer_class {

     public static void main(String[] args) {

        

         StringBuffer STR = new StringBuffer("Hello   All");

         STR.setCharAt(6, '2');

         System.out.println(STR);

     }

}

    

 

OUTPUT:

Hello 2 All

 

 

viii) setLength(): Modifies the length of the string to the specified length:

Ex:

public class HackerRank {

     public static void main(String[] args) {

        

         StringBuffer STR = new StringBuffer("Hello 2 All");

         STR.setLength(20);

 

         STR.setCharAt(19, 'x');

         System.out.println(STR);

         System.out.println(STR.length());

     }

}

    

 

OUTPUT:

Hello 2 All        x

20

 

 

ix) trimToSize() : This method is used to trim the capacity of the string to its size:

Ex:

public class StringBuffer_class {

     public static void main(String[] args) {

        

         StringBuffer STR = new StringBuffer("Hello People");

         System.out.println(STR.capacity());

 

         STR.trimToSize();

 

         System.out.println(STR.capacity());

     }

}

    

 

OUTPUT:

28

12