String Concatenation in Java
In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java:
- By + (String concatenation) operator
- By concat() method
- By + (String concatenation) operator
Java String concatenation operator (+) is utilised to join strings.
Example:
class Example{
//main method
public static void main(String args[]){
//declaring string with str
String str="North"+" America";
System.out.println(str);
}
}
Output:
North America
Functioning of the above code:
String str=(new StringBuilder()).append("Uttar").append(" Pradesh).toString();
In Java, String concatenation is executed via the StringBuilder (or StringBuffer), an append method. The string concatenation operator creates a new string by adding the second operand to the ending of the first operand. This operator can join not only String but primitive values likewise.
Example:
class Example2{
public static void main(String args[]){
String str=20+30+"AddOperation"+20+40;
System.out.println(str);//
}
}
Output:
50AddOperation2040
Note: Behind a string literal, every (+) will be considered a string concatenation operator.
- By concat() method
This method attaches the stipulated String to the ending of the existing String.
Syntax:
public String concat(String another)
Example:
Now, have a look at an eample of the String concat() method.
class Example{
public static void main(String args[]){
String str="North ";
String str2="America";
String str3=s1.concat(s2);
System.out.println(str3);
}
}
Output:
North America
We can conclude that the illustrated Java program joins two String objects, str and str1, with the help of the concat() method and returns the result in the str3 object.
We have several other methods to concatenate strings in Java.
1. String concatenation with the help of StringBuilder class
It is a class that delivers the append() method to achieve concat operations. The append () method abides by various arguments such as Object, StringBuilder, int, char, CharSequence, boolean, float, and double. StringBuilder is the very common and fastest manner to connect strings in Java. It is a variable class. You can update or transform the values kept in the StringBuilder object.
Example:
public class Example
{
public static void main(String args[])
{
StringBuilder str = new StringBuilder("Java"); //String 1
StringBuilder str1 = new StringBuilder(" Tutorial"); //String2
StringBuilder str2 = str1.append(str2); //To store the result
System.out.println(str3.toString());
}
}
Output:
Java Tutorial
In the above code snippet, str1, str2 and str3 are declared objects of the StringBuilder class. s stores the result of the concatenation of str1 and str2 using the append() method.
2. String concatenation using format() method
String.format() method permits concatenating numerous strings utilising format specifiers like %s heeded by the string values or objects.
Example:
public class Example4
{
public static void main(String args[])
{
String str1 = new String("Java"); //String 1
String str2 = new String(" Tutorial"); //String 2
String str3 = String.format("%str3%str3",str1,str2); //String 3 to store the result
System.out.println(str3.toString());
}
}
Output:
Java Tutorial
Here, the String objects str3 are assigned the concatenated result of Strings str1 and str2 using the String.format() method. format() takes parameters as format specifiers heeded by String objects or values.
3. String concatenation using String.join() method (Java Version 8+)
The String.join () method is available in Java version 8 and all of the above. The String.join () method first accepts a delimiter and an array of String objects as arguments.
Example:
public class StrJoin
{
public static void main (String args [])
{
String str1 = new String ("Java"); // String 1
String str2 = new String ("Tutorial"); // String 2
String str3 = String.join (" ", str1, str2); // String to store the result 3
System.out.println (str3.toString ());
}
}
Output:
Java Tutorial
Above In the code snippet, the String object str3 stores the result of the String.join(" ", str1, str2) method. The delimiter is specified by quotation marks, followed by a String object or an array of String objects.
4. String concatenation using StringJoiner class (Java Version 8+)
The StringJoiner class has all the functionality of the String.join() method. In advance, its constructor can also accept optional arguments, prefixes, and suffixes.
Example:
public class StrJoiner
{
public static void main (String args [])
{
StringJoiner str1 = new StringJoiner (","); // StringeJoiner object
str1.add ("Java"); // String 1
str1.add ("Tutorial"); // String 2
System.out.println (str1.toString ());
}
}
Output:
Java, Tutorial
The StringJoiner object str1 is declared and constructed in the above code snippet. The stringJoiner () accepts the separator value. Specify the delimiter by enclosing it in quotation marks. The add () method adds the String passed as an argument.
5. String concatenation using Collectors.joining() method (Java (Java Version 8+)
Java8 Collectors class concatenates and receives input elements in the same order as they occur.
Example:
import java.util.*;
import java.util.stream.Collectors;
public class LastExample
{
public static void main(String args[])
{
List<String> liststr = Arrays.asList("This", "is", "tutorialandexample"); //List of String array
String abc = liststr.stream().collect(Collectors.joining(", ")); //Perform the connection operation
System.out.println(abc.toString()); //Display the result
}
}
Output:
this, is, tutorialandexample
A list of string arrays is declared here. Also, the String object abc stores the result of the Collectors.joining() method.