Java String Concatenation
Java String Concatenation
Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more strings in Java.
- String concatenation using + operator
The arithmetic operator + is placed in between two string variables in order to concatenate those strings. The result can be stored in a third variable or we can directly print it using print statement.
StrConcat.java:
public class StrConcate { /* Driver Code */ public static void main(String args[]) { String s1 = "Hello"; //String 1 String s2 = "World"; //String 2 String s = s1 + s2; //String 3 to store the result System.out.println(s); //Displays result } }
Output:
Hello World
In the above code snippet, String s1 and s2 are concatenated using additionoperator (+) and the result is stored in String s.
- String concatenation using concat() method
The concat() method returns a string object which stores the concatenated result of two different strings.
StrConcate.java:
public class StrConcate { /* Driver Code */ public static void main(String args[]) { String s1 = "Hello"; //String 1 String s2 = " World"; //String 2 String s = s1.concat(s2); //String 3 to store the result System.out.println(s); //Displays result } }
Output:
Hello World
In the above code snippet, the String object s stores the result of s1.concat(s2) method.
- String concatenation using StringBuilder class
StringBuilder is class provides append() method to perform concatenation operation. The append() method accepts arguments of different types like Objects, StringBuilder, int, char, CharSequence, boolean, float, double. StringBuilder is the most popular and fastet way to concatenate strings in Java. It is mutable class which means values stored in StringBuilder objects can be updated or changed.
StrBuilder.java
public class StrBuilder { /* Driver Code */ public static void main(String args[]) { StringBuilder s1 = new StringBuilder("Hello"); //String 1 StringBuilder s2 = new StringBuilder(" World"); //String 2 StringBuilder s = s1.append(s2); //String 3 to store the result System.out.println(s.toString()); //Displays result } }
Output:
Hello World
In the above code snippet, s1, s2 and s are declared as objects of StringBuilder class.s stores the result of concatenation of s1 and s2 using append() method.
- String concatenation using format() method
String.format() method allows to concatenate multiple strings using format specifier like %s followed by the string values or objects.
StrFormat.java
public class StrFormat { /* Driver Code */ public static void main(String args[]) { String s1 = new String("Hello"); //String 1 String s2 = new String(" World"); //String 2 String s = String.format("%s%s",s1,s2); //String 3 to store the result System.out.println(s.toString()); //Displays result } }
Output:
Hello World
Here, the String objects s is assigned the concatenated result of Strings s1 and s2 using String.format() method. format() accepts parameters as format specifier followed by String objects or values.
- String concatenation using String.join() method (Java Version 8+)
The String.join() method is available in Java version 8 and all the above versions.String.join() method accepts arguments as a separator and an array of String objects.
StrJoin.java:
public class StrJoin { /* Driver Code */ public static void main(String args[]) { String s1 = new String("Hello"); //String 1 String s2 = new String(" World"); //String 2 String s = String.join("",s1,s2); //String 3 to store the result System.out.println(s.toString()); //Displays result } }
Output:
Hello World
In the above code snippet, the String object s stores the result of String.join(“”,s1,s2) method. A separator is specified inside quotation marks followed by the String objects or array of String objects.
- String concatenation using StringJoiner class (Java Version 8+)
StrJoiner.java
public class StrJoiner { /* Driver Code */ public static void main(String args[]) { StringJoiner s = new StringJoiner(", "); //StringeJoiner object s.add("Hello"); //String 1 s.add(" World"); //String 2 System.out.println(s.toString()); //Displays result } }
Output:
Hello World
In the above code snippet, the StringJoiner object s is declared and the constructor StringJoiner() accepts a separator value. A separator is specified inside quotation marks. The add() method appends Strings passed as arguments.
- String concatenation using Collectors.joining() method (Java (Java Version 8+)
ColJoining.java
public class ColJoining { /* Driver Code */ public static void main(String args[]) { List<String>liststr = Arrays.asList("abc", "pqr", "xyz"); //List of String array String str = liststr.stream().collect(Collectors.joining(", ")); //performs joining operation System.out.println(str.toString()); //Displays result } }
Output:
abc, pqr, xyz
Here, a list of String array is declared. And a String object str stores the result of Collectors.joining() method.
- Converting String array into String using Arrays.toString() method
Arrays.toString function is used to convert a String array into a String. This method has its definition inside Array class.
ArrtoString.java
import java.util.*; public class ArrtoString { /* Driver Code */ public static void main(String args[]) { String[] arrstr = {"abc", "pqr", "xyz"}; //Array of String objects String str = Arrays.toString(arrstr); //performs conversion operation System.out.println(str.toString()); //Displays result } }
Output:
[abc, pqr, xyz]
Here, a list of String array is declared. And a String object str stores the result of Arrays.toString() method.
In this article, we have discussed different methods available to perform String concatenation in Java. For better understanding each method is explained with an example.