{{announcement.body}}
{{announcement.title}}

How to Convert an Array to String in Java [Snippet]

DZone 's Guide to

How to Convert an Array to String in Java [Snippet]

Check out the code demonstrating different ways to convert arrays or integers to a string.

· Java Zone ·
Free Resource

Using Arrays.toString() Method

Arrays.toString() returns a string with the content of the input array. The new string created is a comma-delimited list of the array’s elements, surrounded with square brackets (“[]”):

// Using Arrays.toString()
    public static String convertArrayToString(String[] strArray) {
        return Arrays.toString(strArray);
    }


Output:

[one, two, three]


Using StringBuilder.append() Method

Let's use the  StringBuilder.append() method to convert an Array to a String:

 // using StringBuilder.append()
    public static String convertArrayToStringMethod(String[] strArray) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < strArray.length; i++) {
            stringBuilder.append(strArray[i]);
        }
        return stringBuilder.toString();
    }


Output:

onetwothree


Using String.join() method

Java 8 and above offers the  String.join() method that produces a new string by joining elements and separating them with the specified delimiter, which, in our case, is just an empty string:

 // Using String.join() method
    public static String convertArrayToStringUsingStreamAPI(String[] strArray) {
        String joinedString = String.join(" ", strArray);
        return joinedString;
    }


Output:

one two three


Using Collectors.joining() Method From the Java Streams API

 // Using Stream API and Collectors
    public static String convertArrayToStringUsingCollectors(String[] strArray) {
        String joinedString = Arrays.stream(strArray)
            .collect(Collectors.joining());

        return joinedString;
    }


Output:

onetwothree


Complete Example

package corejava;

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

public class ArrayToStringConvertExample {
    public static void main(String[] args) {
        String[] strArray = {
            "one",
            "two",
            "three"
        };
        System.out.println(convertArrayToString(strArray));
        System.out.println(convertArrayToStringMethod(strArray));
        System.out.println(convertArrayToStringUsingStreamAPI(strArray));
        System.out.println(convertArrayToStringUsingCollectors(strArray));

        // convert string to array
        String[] strArray1 = "javaguides".split(" ");
    }

    // Using Arrays.toString()
    public static String convertArrayToString(String[] strArray) {
        return Arrays.toString(strArray);
    }

    // using StringBuilder.append()
    public static String convertArrayToStringMethod(String[] strArray) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < strArray.length; i++) {
            stringBuilder.append(strArray[i]);
        }
        return stringBuilder.toString();
    }

    // Using Stream API
    public static String convertArrayToStringUsingStreamAPI(String[] strArray) {
        String joinedString = String.join(" ", strArray);
        return joinedString;
    }

    // Using Stream API and Collectors
    public static String convertArrayToStringUsingCollectors(String[] strArray) {
        String joinedString = Arrays.stream(strArray)
            .collect(Collectors.joining());

        return joinedString;
    }
}


Output:

[one, two, three]
onetwothree
one two three
onetwothree


Similar Examples [Snippets]



If you enjoyed this article and want to learn more about Java Collections, check out this collection of tutorials and articles on all things Java Collections.

Topics:
java ,string ,snippet ,code ,array ,convert ,example

Published at DZone with permission of Ramesh Fadatare . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}