String concat() Method in Java with Examples
The String concat() method concatenates (appends) a string to the end of another string. It returns the combined string. It is used for string concatenation in Java.
It returns NullPointerException if any one of the strings is Null.
Example:
Java
class GFG {
public static void main(String args[])
{
String s = "Geeks " ;
s = s.concat( "for Geeks" );
System.out.println(s);
}
}
|
Syntax
public String concat (String s);
Parameters
A string to be concatenated at the end of the other string
Returns
Concatenated(combined) string
Exception
NullPointerException- When either of the string is Null.
Java String concat() Examples
Below is an example that shows how to use the string concat() function in Java
Example 1:
This example shows the working of the concat method in Java.
java
public class GFG {
public static void main(String args[])
{
String str1 = "Computer-" ;
String str2 = "-Science" ;
String str3 = str1.concat(str2);
System.out.println(str3);
String str4 = "-Portal" ;
String str5 = str3.concat(str4);
System.out.println(str5);
}
}
|
Output
Computer--Science
Computer--Science-Portal
Note: As perceived from the code we can do as many times as we want to concatenate strings bypassing older strings with new strings to be contaminated as a parameter and storing the resultant string in String datatype.
Example 2:
This code shows the NullPointerException in the concat() method.
Java
public class GFG {
public static void main(String args[])
{
String str1 = "Computer-" ;
String str2 = null ;
String str3 = str1.concat(str2);
System.out.println(str3);
}
}
|
Output:
Exception in thread "main" java.lang.NullPointerException
String concat() Method – Java Programs
Let’s see some coding problems and solve them with the String concat() method in Java.
1. Combining two strings using the Java concat() method
Java
class GFG {
public static void main(String args[]) {
String str1 = "To" ;
String str2 = "gether" ;
str1 = str1.concat(str2);
System.out.println(str1);
}
}
|
2. Creating a Reversed String using the Java concat() method
Java
public class ReverseString {
public static void main(String[] args) {
String original = "Geeks" ;
String reversed = "" ;
for ( int i = original.length() - 1 ; i >= 0 ; i--) {
char currentChar = original.charAt(i);
String charAsString = Character.toString(currentChar);
reversed = reversed.concat(charAsString);
}
System.out.println( "Original string: " + original);
System.out.println( "Reversed string: " + reversed);
}
}
|
Output
Original string: Geeks
Reversed string: skeeG
References
To know more about more String Methods refer to the article Java String Methods
Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of concat function and its uses in Java.
The concat method in Java is a fundamental function for string manipulation. With this guide, you can easily perform string concatenation using the concat function.
Last Updated :
16 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...