Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java String Concatenation

Concatenation of strings is the process of combining multiple strings into a single one. Java provides multiple ways to perform string concatenation. Let's learn how to concatenate strings in Java.

Java String Concatenation

Concatenate Strings Using Addition Operator

The simplest way to combine or add strings together is to use the addition operator. This operator is overloaded to work with strings. The following code demonstrates its working.

public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";
	
		String concatString = str1 + str2 + str3;
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: ConcatenateThreeStrings

Concatenate Strings Using StringBuilder

The StringBuilder class in Java is used to represent strings as a mutable array of characters. We can use the append() method of this class to append(or add to the end of) another string to the already present string in the StringBuilder. Finally, we can convert the StringBuilder object to a string by using the toString() method. We must create the StringBuilder object of appropriate size to accommodate the strings.

Suppose, we have three strings, and we want to create a fourth string that is the combination of these three strings. We will use the code below to accomplish this.

public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";
		
		StringBuilder strBuilder = new StringBuilder(40);
		strBuilder.append(str1);
		strBuilder.append(str2);
		strBuilder.append(str3);
		
		String concatString = strBuilder.toString();
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: ConcatenateThreeStrings

Concatenate Strings using String.concat() Method

The String class also provides multiple methods to concatenate strings, one such method is the String.concat(). The code below concatenates three strings into a single one.

public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Strings";
		
		String concatString = str1.concat(str2);
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: ConcatenateStrings

We can also concatenate more than two strings in a single line of code using the concat() method multiple times.

public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";
		String str4 = "Into";
		String str5 = "One";
		
		String concatString = str1.concat(str2).concat(str3).concat(str4).concat(str5);
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: ConcatenateThreeStringsIntoOne

Concatenate Strings using String.format() Method

The format() method of the String class can be used to format strings into a single one. It takes a formatting string and the strings to concatenate as parameters and returns the combined string.

public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";
		
		String concatString = String.format("%s%s%s", str1, str2, str3);
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: ConcatenateThreeStrings

We can also add delimiters in between the strings and use precision modifiers to append just the first few characters of a string. For example, in the code below, we are adding the asterisk as a delimiter and only fetching the first three characters of the third string.

public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";
		
		String concatString = String.format("%s*%s*%.3s", str1, str2, str3);
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: Concatenate*Three*Str

Concatenate Strings using String.join() Method

The String.join() method of Java 8 can be used to concatenate multiple strings into a single one. We also need to pass a delimiter and the combined string will have this delimiter to separate the original strings. We can also pass an empty string as the delimiter if we don't want to add anything else. For example, if the strings to concatenate are "Hello" and "World" and if we pass the delimiter as "-", then the combined string will be "Hello-World".

public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";		
		String concatString = String.join("*", str1, str2, str3);		
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: Concatenate*Three*Strings

Concatenate Strings using StringJoiner Class

The StringJoiner class works just like the join() method discussed above. It is available for Java 8 and above. We need to initialize an object of this class and need to pass a delimiter to its constructor. We can use the add() method to append strings to it. We can get the concatenated string from the StringJoiner object by using the toString() method.

import java.util.StringJoiner;
public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";
		
		StringJoiner sj = new StringJoiner("*");
		sj.add(str1);
		sj.add(str2);
		sj.add(str3);
		
		String concatString = sj.toString();		
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: Concatenate*Three*Strings

We can also pass an optional prefix and suffix to the constructor.

import java.util.StringJoiner;
public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String str1 = "Concatenate";
		String str2 = "Three";
		String str3 = "Strings";
		
		String delimiter = "*";
		String prefix = "##";
		String suffix = "$$";
		StringJoiner sj = new StringJoiner(delimiter, prefix, suffix);		
		sj.add(str1);
		sj.add(str2);
		sj.add(str3);		
		String concatString = sj.toString();		
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: ##Concatenate*Three*Strings$$

Concatenate Strings using Arrays Class

The toString() method of the Arrays class can also be used to convert an array of strings to a single combined string. However, the concatenated string will contain square brackets and the original strings will be separated by a comma. For example, if the array is {"Hello", "Hola", "Ola"} and we use the toString() method on it, then it will return the string "[Hello, Hola, Ola]".

import java.util.Arrays;
public class ConcatenateStrings
{
	public static void main(String[] args)
	{
		String[] strArray = {"Concatenate", "Three", "Strings"};
		String concatString = Arrays.toString(strArray);
		System.out.println("The concatenated string is: " + concatString);
	}
}


The concatenated string is: [Concatenate, Three, Strings]

Summary

Concatenating strings is pretty straightforward in Java. The simplest way to combine strings is by using the addition operator. We also have a lot of other methods and classes that can be used for concatenation in Java. We can also add delimiters to make out the original strings in the concatenated string. Delimiters can be added by using the String.join() method and the StringJoiner class.

An array of several strings can be converted to a single array by using the Arrays.toString() method, but we will end up with unwanted characters in our string. We can concatenate an array of strings by passing the array to the String.join() method.