How to join string array in java

What you will learn here about java

  • How to join string array in java

Here we will see how to join string array in java. It is very easy to join string array using stream. By using stream it is very easy to join string array in java.

How to join string array in java

Below sample program to join string array in java is given:

import java.util.Arrays;
import java.util.stream.Collectors;

public class ArrayStringJoin {
    public static void main(String[] args) {
        String str[] = new String[]{"John", "Smitha", "Sachin"};
        String result = Arrays.stream(str).collect(Collectors.joining(" | "));
        System.out.println("String concated with | : "+result);

        result = Arrays.stream(str).collect(Collectors.joining(" , "));
        System.out.println("String concated with , : "+result);
    }
}

OUTPUT

String concated with | : John | Smitha | Sachin
String concated with , : John , Smitha , Sachin

You may also like...