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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.