Concat Strings in Java

Concat Strings in Java | The word concatenation means to add a string to another string. The concatenation can be done by using the “+” operator. Java string class provides concat() method to add a string to another string. In this blog, let us see the different ways to concatenate strings.

How concatenation of String in Java works:-
Example-1:-
String1 = “Hello”
String2 = “world”
After concatenation:- “Hello world”

Example-2:-
String1 = “Hi”
String2 = “Programmer”
After concatenation:- “Hi Programmer”

We can concatenate strings in two ways:-
a) Using the ‘+’ operator
b) By using concat() method of the Java String class

The syntax of the concat() method is as follows:- public String concat(String str)
Parameters:- str the string needed to be combined.
Returns: the string which is combined.

Java String Concat Example

Example-1:- How to concat string in Java using concat()?

public class Main {
   public static void main(String args[]) {
      String string = "Hi, ";
      string = string.concat("Good Morning");
      System.out.println(string);
   }
}

Output:-

Hi, Good Morning

Let us see another example of how to concat strings in Java using concat() method of string class. This time we will apply concat() method multiple times.

Example-2:- How to concat string in Java using concat()?

public class Main {
   public static void main(String args[]) {
      String string1 = "Java";
      String string2 = " Programming";
      String string3 = " Language";

      String string4 = string1.concat(string2);
      System.out.println("string4: " + string4);
      String string5 = string4.concat(string3);
      System.out.println("string5: " + string5);

      String string6 = string1.concat(string2).concat(string3);
      System.out.println("string6: " + string6);
   }
}

Output:-

string4: Java Programming
string5: Java Programming Language
string6: Java Programming Language

Example-3:- How to concat string in Java using concat()?

public class Main {
   public static void main(String args[]) {
      String str1 = "As string";
      System.out.println(str1);
      str1 = str1.concat(" is immutable.");
      System.out.println(str1);
      str1 = str1.concat(" Once created it cannot be changed.");
      System.out.println(str1);
   }
}

Output:-

As string
As string is immutable.
As string is immutable. Once created it cannot be changed.

Example-4:- How to concat string in Java using concat()?

public class Main {
   public static void main(String[] args) {
      String string1 = "Hello, ";
      String string2 = "KnowProgram";
      String string3 = " Reader's.";

      String string4 = string1.concat(string2);
      System.out.println(string4);

      String str5 = string1.concat(string2).concat(string3);
      System.out.println(str5);
   }
}

Output:-

Hello, KnowProgram
Hello, KnowProgram Reader’s.

How to Concat String in Java using + Operator

Not only using the string class concat() method but also using the “+” operator we can concat strings. Let us understand it through an example-1:-

public class Main {
   public static void main(String args[]) {
      String string = "Hi, ";
      string = string + "Good Morning";
      System.out.println(string);
   }
}

Output:-

Hi, Good Morning

Example-2:- How to concat strings in Java using the + operator?

public class Main {
   public static void main(String args[]) {
      String string1 = "Java";
      String string2 = " Programming";
      String string3 = " Language";

      String string4 = string1 + string2;
      System.out.println("string4: " + string4);
      String string5 = string4 + string3;
      System.out.println("string5: " + string5);

      String string6 = string1 + string2 + string3;
      System.out.println("string6: " + string6);
   }
}

Output:-

string4: Java Programming
string5: Java Programming Language
string6: Java Programming Language

Example-3:- How to concat strings in Java using the + operator?

public class Main {
   public static void main(String args[]) {
      String str1 = "As string";
      System.out.println(str1);
      str1 = str1 + " is immutable.";
      System.out.println(str1);
      str1 = str1 + " Once created it cannot be changed.";
      System.out.println(str1);
   }
}

Output:-

As string
As string is immutable.
As string is immutable. Once created it cannot be changed.

Example-4:- How to concat strings in Java using the + operator?

public class Main {
   public static void main(String[] args) {
      String string1 = "Hello, ";
      String string2 = "KnowProgram";
      String string3 = " Reader's.";

      String string4 = string1 + string2;
      System.out.println(string4);

      String str5 = string1 + string2 + string3;
      System.out.println(str5);
   }
}

Output:-

Hello, KnowProgram
Hello, KnowProgram Reader’s.

Java Concat Array Of Strings

We have seen how to concat strings in Java using concat() method or using the concatenation operator (+). Now let us develop a simple for its use case. We have an array of strings and we have to concat it to make a single string.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      String lang[] = 
         { "Java ", "Python ", "HTML ", "C++ ", "CSS" };
      String string = "";

      for (String str : lang) {
         string = string.concat(str);
      }
      System.out.println("String array: " 
                  + Arrays.toString(lang));

      System.out.println("String Array Merging: " + string);
   }
}

Output:-

String array: [Java , Python , HTML , C++ , CSS]
String Array Merging: Java Python HTML C++ CSS

We can also use the “+” operator to concat array of strings. For this, in for loop we have to use the concatenation operator “+” instead of the Java string concat() method. And we will get the same result.

for (String str : lang) {
   string += str;
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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