Home » Java programming language

Java String concat() Method with Example

Java String concat() Method: Here, we are going to learn about the concat() method with example in Java.
Submitted by IncludeHelp, on February 20, 2019

String concat() Method

concat() is a String method in Java and it is used to concatenate (add) a given string to the string. It returns a concatenated string.

Syntax:

    String str_object.concat(String str);

Here,

  • str is a String object to be added.
  • str_object is the main string in which we have to add/concatenate str.

Method returns a concatenated string at the end of the str_object.

Example:

    Input: 
    str1 = "Hello"
    str2 = "world!"

    Function call:
    str1.concat(str2);

    Output:
    "Helloworld!"

Java code to concatenate the string using String.concat() method

public class Main
{
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "world!";
        
        //adding str2 to the str1
        String str3 = str1.concat(str2);
        
        //printing values
        System.out.println("str1 = " + str1);
        System.out.println("str2 = " + str2);
        System.out.println("str3 = " + str3);
    }
}

Output

str1 = Hello
str2 = world!
str3 = Helloworld!


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.