Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Append Function in Java

Have Programming Question? Ask it here!
 
 
 


Java Coder
Append Function in Java
1 Answer(s)      a year and 7 months ago
Posted in : Java Beginners


What is the use of append function in java? How to use the append function to append a string to a buffer string.

Thanks


Advertisement
View Answers

January 27, 2013 at 2:03 PM


You can use the StringBuffer class in Java to efficiently append the string. This is very efficient method of appending string in Java. Once all the string is appended to the StringBuffer object you can get the final string through calling the toString() on the StringBufferobject. Use StringBuffer is very good and provides good performance if in your application you are appending string multiple times. The class StringBuffer provides the following the necessary function for appending the string.

sb.append("myString");

Here is the complete code of the Java Append example:

import java.io.*;

public class appendFunctionJava 
{
    public static void main(String[] args) 
    {
        System.out.println("Append fuction in Java");

        String finalString; 
        StringBuffer sb = new StringBuffer(100); 
        //Append string
        sb.append("line 1: Example of Append Fuction in Java.\n");
        sb.append("Appended Line 2:\n");
        sb.append("Appended Line 2:\n");

        finalString = sb.toString(); 
        System.out.println("Final String is: " + finalString); 


    }
}

Thanks



Related Tutorials/Questions & Answers:
 

 

 

DMCA.com