In Java-8 we got new class called StringJoiner to join multiple strings. Nothing new in StringJoiner class when we compare with already existing classes like StringBuffer and StringBuilder. Only the difference here we can set prefix and suffix along with delimiter, or we can set only delimiter by using different constructor. Lets see simple example as how to use StringJoiner from Java-8. |
public class StringJoinerExample { public static void main(String[] args) { StringJoiner sJoin = new StringJoiner(","); sJoin.add("java"); sJoin.add("python"); sJoin.add("C#"); sJoin.add("scala"); System.out.println(sJoin); } }
OUTPUT:
java,python,C#,scala
public class StringJoinerExample { public static void main(String[] args) { StringJoiner sJoin = new StringJoiner("," ,"{" ,"}"); sJoin.add("java"); sJoin.add("python"); sJoin.add("C#"); sJoin.add("scala"); System.out.println(sJoin); } }
OUTPUT:
{java,python,C#,scala}
No comments:
Write comments