Java String concat() Method with Examples
The concatenation function in Java makes a new string composed of multiple strings. There are two ways of combining strings in Java: The +string operator is used. Use the concat() method.
String Concatenation by + (String concatenation) operator
The Java string concatenation function is used to insert strings.
Example:
classTechvidvan{ public static void main(String args[]){ String s="Sachin"+" Tendulkar"; System.out.println(s); } }
Output:
Sachin Tendulkar
Concatenation of strings using the concat() method
The string concat() method concatenates a given string to the end of an existing string.
Syntax: public String concatString other
How to concatenate strings in Java
- Concatenation is combining two or more strings to form a new string by appending the next string to the end of the previous strings.
- In Java, two strings can be concatenated using the + or += operator or the concat() method, defined in the java.lang.string class.
- This shot will discuss how to perform string concatenation using both methods.
Example:
The program below demonstrates how to concatenate two strings using the + operator in Java.
class Techvidvan{ public static void main( String args[] ) { String first = "Hello"; String second = "World"; String third = first + second; System.out.println(third); // yet another way to concatenate strings first += second; System.out.println(first); }
Output:
HelloWorld
HelloWorld
Parameter:
stringToConcat is the string to be concatenated. It will be appended at the end of the string object calling this method.
Return type:
The concat() method returns the concatenated string.
String concatenation using StringBuilder class
- To perform concatenation operations, StringBuilder is a class that provides an append() method.
- Arguments of other types, such as objects, string builders, int, char, CharSequence, boolean, float, and double, are accepted by the append() method.
- The most popular and fastest way to concatenate strings in Java is StringBuilder.
- It is an immutable class, which means you can update or change the values in the StringBuilder object.
Example:
public class Techvidvan { public static void main(String args[]) { StringBuilder s1 = new StringBuilder("Hello"); StringBuilder s2 = new StringBuilder(" World"); StringBuilder s = s1.append(s2); System.out.println(s.toString()); } }
Output:
Hello World
s1, s2 and s are defined as StringBuilder objects by the code snippet above. Using an append method, S will store the result by concatenating s1 and s2.
Concatenation of strings using the format() method
The String.format() method can combine multiple strings with a format specification such as %s followed by string values or objects.
Example:
public class Techvidvan { public static void main(String args[]) { String s1 = new String("Hello"); String s2 = new String(" World"); String s = String.format("%s%s",s1,s2); System.out.println(s.toString()); } }
Output:
Hello World
Here, the String s objects are assigned the concatenated result of the strings s1 and s2 using the String.format() method. format() accepts parameters as a format specifier followed by objects or String values.
Concatenate strings using the String.join() method (Java version 8+)
The String.join() method is available in Java version 8 and all versions above. The String.join() method accepts arguments, first a delimiter and an array of String objects.
Example:
public class Techvidvan{ public static void main(String args[]) { String s1 = new String("Hello"); String s2 = new String(" World"); String s = String.join("",s1,s2); System.out.println(s.toString()); } }
Output:
Hello World
The result of the String.join “”,s1,s2″ method will be stored by a string object s in this code snippet. The delimiter is enclosed in a quotation mark next to the string object or array of string objects.
String concatenation using StringJoiner class (Java Version 8+)
StringJoiner class has all the functionalities of String.join() method. In advance, its constructor can also accept optional arguments, prefixes and suffixes.
Example:
public class Techvidvan{ public static void main(String args[]) { StringJoiner s = new StringJoiner(", "); s.add("Hello"); s.add("World"); System.out.println(s.toString()); } }
Output:
Hello, World
A StringJoiner item s is declared in this code snippet, and the stringjoiner constructor takes a separator value. Inside the quotation marks, a separator is indicated. The add() method will append the Strings passed as arguments.
String concatenation using the Collectors.joining (JavaVersion 8+)
The join() function of the Collectors class in Java 8 provides input elements to be grouped into a similar order as they are.
Example:
import java.util.*; import java.util.stream.Collectors; public class Techvidvan { public static void main(String args[]) { List<String> liststr = Arrays.asList("abc", "pqr", "xyz"); String str = liststr.stream().collect(Collectors.joining(", ")); System.out.println(str.toString()); } }
Output:
abc, pqr, xyz
You have a list of string fields. The result of the Collectors.joining() method is held in a String object.
Conclusion
A brief description of Java string concatenation is provided in this article. We’ve also discussed using concat() and “+” operators for strings. We’ve also compared the concat() method and the “+” operator and how to choose one to use in different contexts.