Java String concat() Method with Examples

In this article, we will look at an inbuilt method of the String class called the concat() method in Java programming language. Let’s directly jump into the topic without beating around the bush by looking at the concat() method’s necessity, advantages, and syntax.

Finally, we will look at a couple of codes that illustrate using the concat() method. To understand and master the concept quickly, I recommend you practice each code in the article, modify it, and experiment with it to cover different scenarios.

What does the concat() method In Java do?

The concat() method is an inbuilt method of the JavaString class. This method joins two or more strings together and returns a new string, which is the concatenation of the two or more strings passed as arguments. In a nutshell, the concat() function appends two strings together.

There is nothing more to the concat() method than this. Hence, let us examine its syntax, discuss its parameters and return type, and then jump into some examples to better understand its functionality.

What is the syntax of the concat() method in Java?

Below is the syntax of the concat() method of the Java String class:

String1 concat(String2)

The above syntax concatenates String2 to String1. Here is an example of how to understand the workings and syntax of the concat() method.

class FirstCode
{  
 public static void main(String args[])
 {  
   String s1="First ";  
   String s2="Code";  
   String s3=s1.concat(s2);  
   System.out.println("The concatenated string is: "+s3);
  }  
}

Output:

The concatenated string is: FirstCode

In the above code, the strings “First” and “Code” have been concatenated to “Firstcode” by using the concat() method.

How do you Concatenate multiple strings using the concat() method in Java?

Let us look at more examples of the concat() method. We can also concatenate multiple strings together using the concat() method; here is an example illustrating the same:

public class Main 
{  
    public static void main(String[] args) 
    {      
        String str1 = "Welcome ";  
        String str2 = "To ";  
        String str3 = "First";  
        String str4 = "Code";  
        String str5 = str1.concat(str2).concat(str3).concat(str4);  
        System.out.println(str5);  
    }  
}

Output:

Welcome to FirstCode

In the above code, the strings str1, str2, str3, str4 have been concatenated together by making use of the cpncat() method as “String str5 = str1.concat(str2).concat(str3).concat(str4);”

How do you Concatenate special characters using the concat() method in Java?

Using the concatenate method, we can also append special characters like @, ! %, $, etc, together.

Here is an example:

public class Main 
{  
    public static void main(String[] args) 
    {  
        String str1 = "Hello";  
        String str2 = "FirstCode";  
        String str3 = "Employee";  
        String str4 = str1.concat("!!!").concat(" ").concat(str2).concat(" ").concat(str3);  
        System.out.println(str4);           
        String str6 = str1.concat("@").concat(str2);  
        System.out.println(str6);           
    }  
}

Output:

Hello!!! FirstCode Employee
Hello@FirstCode

How can we append a string at the beginning using the concat() method in Java?

So far, we have only seen that the concat() method adds the string at the end of the string that calls the method. Despite this, we can use a workaround to add the string to the beginning of another string using the concat() method.

Here is one such example:

public class Main  
{  
    public static void main(String argvs[])  
    {  
        String str = "AWESOME!";   
        String s = "FirstCode is ".concat(str);   
        System.out.println(s);  
    }  
}

Output:

FirstCode is AWESOME

Conclusion

In summary, the concat() method in Java, a part of the String class, efficiently combines strings, creating a new string as the output. We have gone through its syntax and demonstrated examples. The concat() method is an essential tool for string manipulation in Java programming.

Mastering this method provides developers with valuable capabilities for concatenating strings, improving code readability, and addressing diverse requirements, such as appending special characters or adding strings at the beginning.

Leave a Reply

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