Convert String Array to String in Java

Java

In this tutorial, we’ll show you few examples of how to convert from a string array to a string in Java.

What You’ll Need

Example 1

Use java.lang.StringBuilder

String[] stringArrays = {"abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"};
StringBuilder builder = new StringBuilder();
for (String str : stringArrays) {
    builder.append(str);
}
System.out.println(builder.toString());

Note: It is recommended to use StringBuilder instead of StringBuffer because it will be faster than StringBuffer under most implementations.

Example 2

Use Java 8 String Join method

String[] stringArrays = {"abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"};
String delimiter = "";
System.out.println(String.join(delimiter, stringArrays));

Example 3

Use Java 8 Streams API

String[] stringArrays = {"abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"};
String string = Stream.of(stringArrays).collect(Collectors.joining());
System.out.println(string);

Java Code Example

StringArrayToStringExample.java

package com.chankok.arrays;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StringArrayToStringExample {

    public static void main(String[] args) {
		
        // Initial the string array
        String[] stringArrays = {"abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"};

        // Example 1
        StringBuilder builder = new StringBuilder();
        for (String str : stringArrays) {
            builder.append(str);
        }
        String string1 = builder.toString();
        System.out.println("Example 1: " + string1);

        // Example 2
        String delimiter = "";
        String string2 = String.join(delimiter, stringArrays);
        System.out.println("Example 2: " + string2);

        // Example 3
        String string3 = Stream.of(stringArrays).collect(Collectors.joining());
        System.out.println("Example 3: " + string3);

    }

}

Output

Example 1: abcdefghijklmnopqrstuvwxyz
Example 2: abcdefghijklmnopqrstuvwxyz
Example 3: abcdefghijklmnopqrstuvwxyz