Java – String concat(String str) method

In this article, we will discuss how to concatenate two strings or multiple strings using String’s concat() method

1. String’s concat (String str) method:

  • This String method concatenates the specified string at the end of the current string

1.1 Method Signature:

public String concat(String str);

1.2 Returns:

  • Returns concatenated or combined string

2. Examples on concat() method:

StringConcatMethod.java

package in.bench.resources.string.methods;

public class StringConcatMethod {

	public static void main(String[] args) {

		String str1 = "Bench";

		// concatenating str1 with string literal
		str1.concat("Resources");

		// printing to the console - str1
		System.out.println(str1);

		// again, concatenating - need to assign it explicitly
		str1 = str1.concat("Resources.Net");

		// printing to the console - str1
		System.out.println(str1);

		// concatenating multiple strings, at once
		String str2 = "This weblog has".concat(" lot of")
				.concat(" Java tutorials");

		// printing to the console - str2
		System.out.println(str2);
	}
}

Output:

Bench
BenchResources.Net
This weblog has lot of Java tutorials

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String contains(CharSequence s) method with example
Java - String compareToIgnoreCase(String anotherString) method