Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java String: concat() Method

public String concat(String str)

The concat() method is used to concatenate a given string to the end of another string.

If the length of the argument string is 0, then this String object is returned.

Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

Java Platform: Java SE 8

Syntax:

concat(String str)

Parameters:

Name Description Type
str the String that is concatenated to the end of this String. String

Return Value:
a string that represents the concatenation of this object's characters followed by the string argument's characters.
greater than, equal to, or less than this String, ignoring case
considerations.

Return Value Type: string

Pictorial presentation of Java String concat() Method

Java String: concat() Method

Example: Java String concat() Method

public class Example {

public static void main(String[] args)
    {
        String str1 = "PHP Exercises and ";
        String str2 = "Python Exercises";
System.out.println();
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2); 


        // Concatenate the two strings together.
        String str3 = str1.concat(str2);

        // Display the new String.
System.out.println("The concatenated string: " + str3);
System.out.println();
    }
}

Output:

String 1: PHP Exercises and                            
String 2: Python Exercises                             
The concatenated string: PHP Exercises and Python Exercises

Java Code Editor:

Previous:compareToIgnoreCase Method
Next:contains Method