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. |
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.
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); } }
Hello how are you
length() − This method returns the length of the current Joiner i.e. number characters in it including the delimiter.
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()); } }
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.
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); } }
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.
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); } }
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); } }
Hello how are you