java String

String concat() in Java Code With Example

Table of Contents

Program

public class StringConcat 
{
	public static void main(String args[]) 
	{
		String s1 = "I am";
		System.out.println(s1);
		s1 = s1.concat(" a good boy");
		System.out.println(s1);
	}
}  

Output

I am
I am a good boy

Description

public String concat​(String str)

Concatenates the specified string to the end of this 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.

Examples:

“cares”.concat(“s”) returns “caress”
“to”.concat(“get”).concat(“her”) returns “together”

Parameters:

str – the String that is concatenated to the end of this String.

Returns:

a string that represents the concatenation of this object’s characters followed by the string argument’s characters.