- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
StringJoiner Class vs String.join() Method to Join String in Java
There are numerous ways of joining strings in java. The StringJoiner class and String.join() method are two of them. Both are used to join two or any number of strings but the difference lies in their implementation.
In this article, we will try to find the differences that exist in the usage of StringJoiner class and String.join() method.
StringJoiner Class
It is a class available in java.util package and it is added from JDK 8. We can join multiple strings at a time.
Before using, it is necessary to import it into our program using the following command −
import java.util.StringJoiner;
We can use this class in two ways −
Syntax
StringJoiner nameOfinstance = new StringJoiner(" delimiter "); Or, StringJoiner nameOfinstance = new StringJoiner(" delimiter ", “ prefix ”, “ suffix ”);
Parameters
delimiter − characters like comma (,), pipe symbol (|) or colon (:) which can be used to differentiate the regions of different strings.
Suffix − we can provide closing brackets. It will be added at the end.
Prefix − we can provide opening brackets. It will be added at the start.
Let’s discuss with few examples.
Example 1
In this example, we will create an instance ‘toJoin’ of StringJoiner class along with the delimiter only to separate the strings. We will use pipe symbol as delimiter and add() method of this class that joins the passed strings arguments to the ‘toJoin’.
import java.util.StringJoiner; public class Joiner { public static void main(String []args) { StringJoiner toJoin = new StringJoiner(" | "); String st1 = "Tutorials"; String st2 = "Tutorialspoint"; String st3 = "Tutorix"; toJoin.add(st1); toJoin.add(st2); toJoin.add(st3); System.out.println("Concatenated string: " + toJoin); } }
Output
Concatenated string: Tutorials | Tutorialspoint | Tutorix
Example 2
We can also add all the strings in one line.
import java.util.StringJoiner; public class Joiner { public static void main(String []args) { StringJoiner toJoin = new StringJoiner(" | ").add("Tutorials").add("Tutorialspoint").add("Tutorix"); System.out.println("Concatenated string: " + toJoin); } }
Output
Concatenated string: Tutorials | Tutorialspoint | Tutorix
Example 3
The following example illustrates the use of suffix and prefix along with delimiter. We will use the opening and closing square brackets.
import java.util.StringJoiner; public class Joiner { public static void main(String []args) { StringJoiner toJoin = new StringJoiner(" | ", " [ ", " ] "); String st1 = "Tutorials"; String st2 = "Tutorialspoint"; String st3 = "Tutorix"; toJoin.add(st1); toJoin.add(st2); toJoin.add(st3); System.out.println("Concatenated string: " + toJoin); } }
Output
Concatenated string: [ Tutorials | Tutorialspoint | Tutorix ]
String.join() Method
The following point differentiates String.join() method from StringJoiner class −
String.join() is a predefined method of String class that is available in java.lang package.
There is no need to import it before using because by default it is available for our use.
We don’t need to create any instance as we do for StringJoiner because it is a method.
It takes only delimiter as parameter.
It can work with strings, string arrays and collection interfaces.
Syntax
String nameOfvariable = String.join(" delimiter ", " val1 ", " val2 "); Or, String nameOfvariable = String.join(" delimiter ", nameOflist);
Parameters
delimiter − characters like comma (,), pipe symbol (|) or colon (:) which can be used to differentiate the regions of different strings.
val − The values going to add.
nameOflist − If we have our separate string array or list of strings then we pass it without quotes.
Example 1
In this example, we will create a string variable ‘toJoin’ that will store the result from String.join() method. We will use commas as our delimiter.
public class Joiner { public static void main(String[] args) { String toJoin = String.join(" , ", "Tutorials", "Tutorialspoint", "Tutorix"); System.out.print("Concatenated string: " + toJoin); } }
Output
Concatenated string: Tutorials , Tutorialspoint , Tutorix
Example 2
The following example illustrates how we can join the string type elements of a list. First, we will create a list ‘araylist’ of string-type with three values and then we will pass it to String.join() method to join all the elements.
import java.util.*; public class Joiner { public static void main(String []args) { ArrayList<String> araylist = new ArrayList<String>(); araylist.add("Tutorials"); araylist.add("Tutorialspoint"); araylist.add("Tutorix"); String toJoin = String.join(" , ", araylist); System.out.print("Concatenated string: " + toJoin); } }
Output
Concatenated string: Tutorials , Tutorialspoint , Tutorix
Conclusion
In this article, we have differentiated StringJoiner class from String.join() method. One is a class and other one is an inbuilt method. We have seen different examples of their use case in java programs.