AppDividend
Latest Code Tutorials

Concat() In Java Example | Java String Concat() Method

0

Concat() In Java Example | Java String Concat() Method is today’s topic. Java string concat method is used to concatenate two or more than two strings to a single string. This method mainly appends the string at the end of the main string. For example: If we have two strings, “Hello” and “World” and we want to concatenate them, then the result will be HelloWorld.”

Concat() In Java Example

In Java language, the string concatenation forms the new string that is a combination of the multiple strings. There are two ways to concat the string in Java:

  1. By + (string concatenation) operator
  2. By concat() method

Java String concat() method concatenates one string to an end of another string. The concat() method returns a string with the value of the string passed into the method, appended to the end of the string.

Syntax

public String concat(String str)

Here str is the string to be concatenated at the end of the first string.

The method returns a single string by concatenating more than one strings.

Example Of String Concatenation

Concatenate two strings

See the following code.

class Concat {
	public static void main(String args[]) {
		String s1 = "Hello everyone ";
		String s2 = "welcome to AppDividend";
		// We are using concat()
		s1 = s1.concat(s2); // will append s2 at the end of s1
		System.out.println(s1);
	}
}

See the output.

Concat() In Java Example

Concatenate more than two strings

See the following code.

class Concat {
	public static void main(String args[]) {
		String s1 = "Hello everyone ";
		String s2 = "welcome to";
		// We are using concat()
		// will append s2 at the end of s1 and then AppDividend
		s1 = s1.concat(s2).concat(" AppDividend");
		System.out.println(s1);
	}
}

See the output.

Java String Concat() Method

Java String Concat() Method Program

See the following code.

public class ConcatEx {
	public static void main(String[] args) {
		String str1 = "Hello";
		String str2 = "AppDividend";
		String str3 = "Reader";
		
                // Concatenating Space among strings
		String str4 = str1.concat(" ").concat(str2).concat(" ").concat(str3);
		System.out.println(str4);
		
                // Concatenating Special Chars
		String str5 = str1.concat("!!!");
		System.out.println(str5);
		String str6 = str1.concat("@").concat(str2);
		System.out.println(str6);
	}
}

See the output.

Hello AppDividend Reader
Hello!!!
Hello@AppDividend

Finally, Concat() In Java Example | Java String Concat() Method is over.

Recommended Posts

Java Method Overloading Example

Wrapper Class in Java Tutorial

Java Object Class Tutorial With Example

this keyword in Java Tutorial With Example

Runnable Interface in Java Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.