Saved Bookmarks
| 1. |
How to convert an Array to String in Java? |
|
Answer» An array can be CONVERTED to a string in four different ways such as Arrays.toString() method, String.Join() method, StringBuilder.append() method, and Collectors.joining() method. Here, we will see an example of the Array.toString() method. Arrays.toString() returns a string representation of the array contents. The string represents the array's elements as a list, enclosed in square brackets ("[]"). The characters ", " (a comma) FOLLOWED by a space are used to separate adjacent elements. It returns “null” if the array is null. import java.util.Arrays;PUBLIC class ArrayToString { public static void main(String[] args) { String[] strArray = { "Scaler", "by", "InterviewBit"}; String str1 = ConvertArraytoString(strArray); System.out.println("An array converted to a string: " + str1); } // Using the Arrays.toString() method public static String ConvertArraytoString(String[] strArray) { return Arrays.toString(strArray); }}Output: An array converted to a string: [Scaler, by, InterviewBit] |
|