Java.lang.StringBuilder.append() Method

Advertisements


Description

The java.lang.StringBuilder.append(String str) method appends the specified string to this character sequence.The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument.

Declaration

Following is the declaration for java.lang.StringBuilder.append() method

public StringBuilder append(String str)

Parameters

  • str -- This is a string.

Return Value

This method returns a reference to this object.

Exception

  • NA

Example

The following example shows the usage of java.lang.StringBuilder.append() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBuilderDemo {

  public static void main(String[] args) {
  
    StringBuilder str = new StringBuilder("tutorials ");
    System.out.println("string = " + str);
    
    // appends the string argument to the StringBuilder
    str.append("point");
    // print the StringBuilder after appending
    System.out.println("After append = " + str);
    
    str = new StringBuilder("1234 ");
    System.out.println("string = " + str);
    // appends the string argument to the StringBuilder 
    str.append("!#$%");
    // print the StringBuilder after appending
    System.out.println("After append = " + str);
  }
} 

Let us compile and run the above program, this will produce the following result:

string = tutorials
After append = tutorials point
string = 1234
After append = 1234 !#$%



Advertisements
Advertisements