Java String concat() Method

Java String Concat() Method

What is String?

In Java, an string class is a series of characters that are interpreted literally by a script. An object called a string represents a string of characters. Java.lang is used. A string object is made using the String class.

  • Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.

To know more about String Class in java read the complete article.

String concat() Method:

The Java String class concat() method combines strings and returns a combined string. Multiple strings are concatenated using the Java string concat() function. The combined string is returned after the requested string is appended to the end of the original string using this function. To merge multiple strings together, we can utilise the concat() technique.

  • The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.

Syntax:

public String concat(String str)

Parameters:

str - the String that is concatenated to the end of this String.

Let’s look at a string-related Java program where the Java String concat() method is used to perform an operation on the given string.

Examples : Concatenating two given strings difference

Run

public class Main {
  public static void main(String[] args) {
    String firstName = "PrepInsta";
    String lastName = "Prime";
    System.out.println(firstName.concat(lastName));
  }
}

Output

PrepInstaPrime
Java String Concat() Method

Examples 2 : Java String Concat() Method

Run

public class Main {
   public static void main(String args[]) {
       //One way of doing concatenation
       String str1 = "Welcome";
       str1 = str1.concat(" to ");
       str1 = str1.concat(" PrepInsta ");
       System.out.println(str1);       
   }
}

Output

Welcome to  PrepInsta 

Examples 3: Concatenating two given strings ignoring lower and upper case.

Run

public class Main {  
    public static void main(String[] args) {      
        String str1 = "Hey Everyone, ";  
        String str2 = "Welcome to PrepInsta";  
        String str3 = " Prime";  
        String str4 = str1.concat(str2);          
        System.out.println(str4);  
        String str5 = str1.concat(str2).concat(str3);  
        System.out.println(str5);  
    }  
}  

Output

Hey Everyone, Welcome to PrepInsta
Hey Everyone, Welcome to PrepInsta Prime

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription