Java : String Concatenation


 

Java : String Concatenation

This tutorial will teach you how to concatenate two strings using example.

This tutorial will teach you how to concatenate two strings using example.

Java : String Concatenation

This tutorial will teach you how to concatenate two strings using example.

String concat() method :  Apart from replacing, removing splitting the string, Java also provides a method to concatenate the string, concat(). This method returns the string that represents the concatenation of the object's characters followed by the string argument's characters. It appends appends one String to the end of another.

Example :

class StringConcatenation {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Roseindia";
String str;
str = str1.concat(str2); // Concatenation of two strings
System.out.println("After concatenation String: " + str);
}
}

Description : In this example, we have defined two string variables and pass values to the variables. Now to concatenate both the strings, we have used concat() method that takes the strings and append one string to the end of the another. You can concatenate more than two strings also.

Output :

After concatenation String: HelloRoseindia

Ads