Java.lang.String.concat() Method

Advertisements


Description

The java.lang.String.concat() method concatenates the specified string to the end of this string.

Declaration

Following is the declaration for java.lang.String.concat() method

public String concat(String str)

Parameters

  • str -- This is the String that is concatenated to the end of this String.

Return Value

This method returns a string that represents the concatenation of this object's characters followed by the string argument's characters.

Exception

  • NA

Example

The following example shows the usage of java.lang.String.concat() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    // print str1
    String str1 = "self";
    System.out.println(str1);
 
    // print str2 concatenated with str1
    String str2 = str1.concat(" learning");
    System.out.println(str2);
   
    // prints str3 concatenated with str2(and str1)
    String str3 = str2.concat(" center");
    System.out.println(str3);
  }
}

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

self
self learning
self learning center



Advertisements
Advertisements