Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • StringBuffer in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 339
    Comment on it

    StringBuffer:

    As we know in Java String is immutable means we cannot reassign a new value to the same string object. That's why in Java to provide mutability we use either StringBuffer or StringBuilder. StringBuffer is a class in Java which provide mutability as we can modify the string object value.

    StringBuffer constructors:

     StringBuffer() // It creates an empty string buffer with capacity of 16.
     StringBuffer(String str)// It creates a string buffer with string provided.
     StringBuffer(int capacity)
    

    Some of the methods of StringBuffer:

    1.append():

    It is used to add some arguments with the string.

    Example:

    class Buffer{  
    public static void main(String args[]){  
    StringBuffer obj=new StringBuffer("Welcome ");  
    obj.append("to FindNerd");//appending more characters to string  
    System.out.println(obj); //prints Welcome to FindNerd
    }  
    }  
    

    2. reverse():

    It reverses the string on which reverse method will be used.

    Example:

       class Buffer{  
        public static void main(String args[]){  
        StringBuffer obj=new StringBuffer("Welcome ");  
       obj.reverse();  
       System.out.println(obj);//prints emocleW  
    }  
    }  
    

    3.insert():

    This method is used to insert string provided at a particular index.

    Example:

    class Buffer{  
        public static void main(String args[]){  
        StringBuffer obj=new StringBuffer("Welcome ");  
        obj.insert(1,"Java");//now original string is changed  
        System.out.println(obj);//prints WJavaelcome  
        }  
        }  
    

    4. replace():

    It replaces string from the specified StartingIndex and EndIndex.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: