- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Explain about StringJoiner in java8?
Since Java8 StringJoiner class is introduced this you can construct a sequence of characters separated by desired delimiter.
Sr.No | Constructor & Description |
---|---|
1 | StringJoiner(CharSequence delimiter) This constructor creates an empty (no characters) StringJoiner, just with a copy of the specified delimiter. |
2 | StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) This constructs a StringJoiner without characters. |
Methods of StringJoiner class
add() − This method accepts a CharacterSequence object (Segment, String, StringBuffer, StringBuilder) and adds it to the current Joiner separating the next and previous elements (if any) with delimiter at the time of constructing it.
Example
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String args[]) { StringJoiner joiner = new StringJoiner(" "); joiner.add("Hello"); joiner.add("how"); joiner.add("are"); joiner.add("you"); System.out.println(joiner); } }
Output
Hello how are you
length() − This method returns the length of the current Joiner i.e. number characters in it including the delimiter.
Example
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String args[]) { StringJoiner joiner = new StringJoiner(" "); joiner.add("Hello"); joiner.add("how"); joiner.add("are"); joiner.add("you"); System.out.println("Contents: "+joiner); System.out.println("length: "+joiner.length()); } }
Output
Contents: Hello how are you length: 17
merge() − This method accepts a StringJoiner object, adds/concatenates it to the current one and returns the resultant StringJoiner object.
Example
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String args[]) { StringJoiner joiner1 = new StringJoiner(" "); joiner1.add("Hello"); joiner1.add("how"); joiner1.add("are"); joiner1.add("you"); StringJoiner joiner2 = new StringJoiner(" "); joiner2.add("Welcome"); joiner2.add("to"); joiner2.add("Tutorialspoint"); StringJoiner result = joiner1.merge(joiner2); System.out.println("Contents: "+result); } }
Output
Contents: Hello how are you Welcome to Tutorialspoint
setEmptyValue() − This method accepts an CharacterSequence object as a parameter and sets it as a value of empty Sting for the current StringJoiner object i.e. if no elements are added to a StringJoiner object if you print its contents the value you have set using this method will be displayed. In short, once you invoke this method on a StringJoiner object it is never considered empty.
Example
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String args[]) { StringJoiner joiner = new StringJoiner(" "); joiner.setEmptyValue("sample empty value"); System.out.println(joiner); } }
Output
sample empty value
toString() − This method returns the contents of the current StringJoiner as a Sting object.
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String args[]) { StringJoiner joiner = new StringJoiner(" "); joiner.add("Hello"); joiner.add("how"); joiner.add("are"); joiner.add("you"); String value = joiner.toString(); System.out.println(value); } }
Output
Hello how are you
- Related Articles
- Java.util.StringJoiner in Java8
- Explain about Varargs in java?
- Explain about derivatives in finance.
- StringJoiner add() method in Java 8
- StringJoiner setEmptyValue() method in Java 8
- StringJoiner toString() method in Java 8
- StringJoiner merge() method in Java 8
- StringJoiner length() method in Java 8
- Explain about financial strategy.
- Explain about Transfer pricing
- Explain about field hiding in java?
- Explain about bitwise operators in JavaScript?
- Explain about financial system in India.
- Explain about modelling in financial management.
- Explain about forecasting in financial management.
