String Concatenation in Java

In Java, String concatenation forms a new String that is the combination of multiple strings. There are two ways to concatenate strings in Java:

  1. By + (String concatenation) operator
  2. By concat() method

1) String Concatenation by + (String concatenation) operator

Java String concatenation operator (+) is used to add strings. For Example:

TestStringConcatenation1.java

  1. class TestStringConcatenation1{
  2.  public static void main(String args[]){
  3.    String s=“Virat “+” Kohli”;
  4.    System.out.println(s);//Sachin Tendulkar
  5.  }
  6. }

 

Output:

Virat Kohli

The Java compiler transforms above code to this:

  1. String s=(new StringBuilder()).append(“Virat”).append(” Kohli).toString();

In Java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and it’s append method. String concatenation operator produces a new String by appending the second operand onto the end of the first operand. The String concatenation operator can concatenate not only String but primitive values also. For Example:

TestStringConcatenation2.java

  1. class TestStringConcatenation2{
  2.  public static void main(String args[]){
  3.    String s=50+30+“Virat”+40+40;
  4.    System.out.println(s);//80Virat4040
  5.  }
  6. }

 

Output:

80virat4040

Note: After a string literal, all the + will be treated as string concatenation operator.

2) String Concatenation by concat() method

The String concat() method concatenates the specified string to the end of current string. Syntax:

  1. public String concat(String another)

Let’s see the example of String concat() method.

TestStringConcatenation3.java

  1. class TestStringConcatenation3{
  2.  public static void main(String args[]){
  3.    String s1=“Virat “;
  4.    String s2=“Kohli”;
  5.    String s3=s1.concat(s2);
  6.    System.out.println(s3);//Virat Kohli
  7.   }
  8. }

 

Output:

Virat Kohli

The above Java program, concatenates two String objects s1 and s2 using concat() method and stores the result into s3 object.

There are some other possible ways to concatenate Strings in Java,

1. 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

  1. public class StrBuilder
  2. {
  3.     /* Driver Code */
  4.     public static void main(String args[])
  5.     {
  6.         StringBuilder s1 = new StringBuilder(“Hello”);    //String 1
  7.         StringBuilder s2 = new StringBuilder(” World”);    //String 2
  8.         StringBuilder s = s1.append(s2);   //String 3 to store the result
  9.             System.out.println(s.toString());  //Displays result
  10.     }
  11. }

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.

2. 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

  1. public class StrFormat
  2. {
  3.     /* Driver Code */
  4.     public static void main(String args[])
  5.     {
  6.         String s1 = new String(“Hello”);    //String 1
  7.         String s2 = new String(” World”);    //String 2
  8.         String s = String.format(“%s%s”,s1,s2);   //String 3 to store the result
  9.             System.out.println(s.toString());  //Displays result
  10.     }
  11. }

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.

3. 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 first a separator and an array of String objects.

StrJoin.java:

  1. public class StrJoin
  2. {
  3.     /* Driver Code */
  4.     public static void main(String args[])
  5.     {
  6.         String s1 = new String(“Hello”);    //String 1
  7.         String s2 = new String(” World”);    //String 2
  8.         String s = String.join(“”,s1,s2);   //String 3 to store the result
  9.             System.out.println(s.toString());  //Displays result
  10.     }
  11. }

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.

4. String concatenation using StringJoiner class (Java Version 8+)

StringJoiner class has all the functionalities of String.join() method. In advance its constructor can also accept optional arguments, prefix and suffix.

StrJoiner.java

  1. public class StrJoiner
  2. {
  3.     /* Driver Code */
  4.     public static void main(String args[])
  5.     {
  6.         StringJoiner s = new StringJoiner(“, “);   //StringeJoiner object
  7.         s.add(“Hello”);    //String 1 
  8.         s.add(“World”);    //String 2
  9.         System.out.println(s.toString());  //Displays result
  10.     }
  11. }

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.

5. String concatenation using Collectors.joining() method (Java (Java Version 8+)

The Collectors class in Java 8 offers joining() method that concatenates the input elements in a similar order as they occur.

ColJoining.java

  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. public class ColJoining
  4. {
  5.     /* Driver Code */
  6.     public static void main(String args[])
  7.     {
  8.         List<String> liststr = Arrays.asList(“abc”“pqr”“xyz”); //List of String array
  9.     String str = liststr.stream().collect(Collectors.joining(“, “)); //performs joining operation
  10.         System.out.println(str.toString());  //Displays result
  11.     }
  12. }

Output:

abc, pqr, xyz

Here, a list of String array is declared. And a String object str stores the result of Collectors.joining() method.


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *