CodeGym /Java Blog /Strings in Java /Array to String in Java
Author
Milan Vucic
Programming Tutor at Codementor.io

Array to String in Java

Published in the Strings in Java group
In the world of Java programming, there's a common challenge that often puzzles beginners and seasoned coders alike: converting an array to a string. In this article, we'll explore various ways to turn that bunch of elements in an array into a neat, readable string. Array to String in Java - 1

How to Convert an Array to String in Java

Let's kick things off with the basics. Converting an array to a string in Java might sound like a no-brainer, but there are a few twists and turns along the way. Whether you've got an array of integers, characters, or even objects, Java provides you with some nifty tools to make this conversion easy.

Array to String Example

Let's dive into some examples to see how this magic happens. We'll start with a simple scenario — converting a Java string array to string. Suppose you've got an array of strings, and you want to merge them into a single string. The straightforward way is to use the Arrays.toString() method. Here's a quick example of converting Java string array to string:

import java.util.Arrays;

public class ArrayToStringExample {

 public static void main(String[] args) {
 String[] strArray = {"One", "Two", "Three"};
 String arrayToString = Arrays.toString(strArray);
 System.out.println("Array to String: " + arrayToString);
 }

}
Here is the output:
Array to String: [One, Two, Three]
Looks pretty easy, right? But wait, there's more! What if you want to convert an array of integers, or, let's say, any other primitive type, to a string? No worries, Arrays.toString() method has got your back.

public class IntegerArrayToString {

 public static void main(String[] args) {
 int[] intArray = {1, 2, 3, 4, 5};
 String arrayToString = Arrays.toString(intArray);
 System.out.println("Array to String: " + arrayToString);
 }
}
And voilà! You'll get something like this:
Array to String: [1, 2, 3, 4, 5]

Custom Conversions: Beyond the Basics

Sometimes, you might need a bit more control over how you convert the array to string. Maybe you're not a fan of those square brackets, or you need a specific separator between elements. In such cases, Java's String.join() and good old loops come to the rescue. Let's say you want to convert a Java string array to a string but with a twist — you want to separate each element with a dash. Here's how you can do it:

public class CustomArrayToString {

 public static void main(String[] args) {
 String[] strArray = {"Java", "String", "Array", "To", "String"};
 String arrayToString = String.join("-", strArray);
 System.out.println("Java String Array to String: " + arrayToString);
 }
}
Output:
Java String Array to String: Java-String-Array-To-String

Convert an Array to String Manually

For the ultimate control, you might want to convert an array to string manually. This is where loops come into play. Here's a simple for-loop to concatenate string elements:

public class ArrayToStringEx {


       public static void main(String[] args) {
           String[] strArray = {"Convert", "an", "array", "to", "string"};
           String result = "";


           for (String element : strArray) {
               result += element + " ";
           }


           result = result.trim();
           System.out.println("Convert Array to String: " + result);
       }
      
}

}
And there you have it:
Convert Array to String: Convert an array to string
What happens here? In Java, when you use the + with strings, it's like a glue – it sticks them together. Now, about turning an array into a string – the + isn't a one-step wonder for this. It doesn't transform the whole array into a string by itself. Instead, you use it to add each piece of your array, one by one, to a growing string. Imagine you're on a treasure hunt, picking up words (array elements) and adding them to your treasure chest (the string) as you go along. So, the + is your go-to for linking strings together. To make a whole array into a single string, you just loop through the array, using + to add each element to your string, and voilà – you get a big, nice string!

Conclusion

Converting an array to string in Java doesn't have to be a head-scratcher. Whether you go for the simplicity of Arrays.toString(), the customization of String.join(), or the flexibility of a manual approach, Java offers a solution for every scenario. So, go ahead and turn those arrays into strings with ease!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION