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

 

concat (String str) method: This String method concatenates the specified string at the end of the current string

 

Method Signature:

public String concat(String str);

 

Returns:

Returns concatenated or combined string

 

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

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Read Also:

  • Java: String class
  • Java: String Literal and String constant pool concept
  • Java: String is Immutable
  • Java: String comparison
  • Java: String concatenation

 

Happy Coding !!
Happy Learning !!