Java StringJoiner Example

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

Java StringJoiner Example

The following example joins numbers with the StringJoiner class.
import java.util.StringJoiner;

public class StringJoinerDemo {

    public static void main(String[] args) {

        StringJoiner join = new StringJoiner(",");
        join.add("1");
        join.add("2");
        join.add("3");
        join.add("4");
        join.add("5");
        join.add("6");

        System.out.println(join);
    }
}
Output:
1,2,3,4,5,6

Reference


Comments