Java String - concat() Method
The java.lang.String.concat() method returns the string which contains the specified string added to the end of the given string.
Syntax
public String concat(String str)
Parameters
str |
Specify a string value which need to be added to the given string. |
Return Value
Returns the concatenated version of the specified string.
Example:
In the example below, concat() method is used to add the string called str2 to the end of the string called str1.
import java.lang.*; public class MyClass { public static void main(String[] args) { String str1 = "Hello"; String str2 = " World!."; System.out.println(str1.concat(str2)); } }
The output of the above code will be:
Hello World!.
Example:
The example below shows how to concatenate more than two string using concat() method.
import java.lang.*; public class MyClass { public static void main(String[] args) { String str1 = "Learning"; String str2 = " Java"; String str3 = " is fun."; //concatenate all three strings String str = str1.concat(str2).concat(str3); //print the final string System.out.println(str); } }
The output of the above code will be:
Learning Java is fun.
❮ Java.lang - String