In this article, we’ll look at the String array convert to String. This can be useful for example to log query parameters.
String.join method example
From Java 8, the simplest way do convert is:
1 2 3 4 |
String[] array = {"dog", "cat"}; String delimiter = " "; String result = String.join(delimiter, array); System.out.println(result); |
StringBuilder example
StringBuilder is thread safe and getting more perfomance benefit over simple String concatenation
1 2 3 4 5 6 7 |
String[] array = {"dog", "cat"}; StringBuilder builder = new StringBuilder(); for (String s : array) { builder.append(s); } String str = builder.toString(); System.out.println(builder); |