BlogsDope image BlogsDope

Java String Concat() method

March 5, 2021 STRING FUNCTION JAVA 161

The java string concat() method is used to combine strings or we can say that this method appends the specified string at the end of the given string.

We can combine more than two strings using concat() method.

Java Concat() Example

  • ​Input: "hello".concat("world")

       Output: helloworld       //concatenated both the strings

  • Input: "I".concat("love").concat("coding")

       Output: Ilovecoding      //we can concatenate multiple strings

Java Concat() Syntax

string.concat(String str)

Concat() Parameter

There is only one parameter used in this method-

str- another string that is needed to be joined.

concat() Return

This method returns a combined or concatenated string.

Program

package codesdope;
import java.util.*;
public class codesdope 
{
	public static void main(String[] args) 
	{
		String s1="codesdope";
		String s2="google";
		System.out.println(s1.concat(".com"));              //case-1
		System.out.println(s2.concat(".com"));              //case-2
		System.out.println(s1.concat(s2).concat(".com"));    //case-3
	}
}

​codesdope.com 

google.com 

codesdopegoogle.com

  • In the first case, when we have applied the concat() method on the strings s1 i.e., "codesdope" and ".com", it returned "codesdope.com" (concatenated both the strings).
  • In the second case, when we have applied concat() method on the strings s2 i.e., "google" and ".com", it returned "google.com" (concatenated both the strings).
  • In the last case, when we have applied concat() method on the strings s1 i.e., "codesdope" , s2 i.e., "google" and ".com", it returned "codesdopegoogle.com" (concatenated all the strings).

Liked the post?
Editor's Picks
0 COMMENT

Please login to view or add comment(s).