Combine two strings in Java

concat(String anotherString) method of String class can be used to join or combine two strings in Java.

concat(String astring) method joins the end of the string with another string and returns the combined string.


Example 1

class StringTest{

public static void main(String args[]){

String str1=new String("1234");

String str2=new String("abcd");

String str3=str1.concat(str2);

System.out.println("Joined string is "+str3);

}

}

Output 1

Joined string is 1234abcd


Example 2

class StringTest{

public static void main(String args[]){

String name=new String("ProgramZools");

String verb=new String(" is ");

String character=new String("very good");

String sentence=name.concat(verb);

sentence=sentence.concat(character);

System.out.println(sentence);

}

}

Output 2

ProgramZools is very good