SoFunction
Updated on 2025-04-22

Several common ways to convert arrays and Strings in Java

In Java, arrays andStringThe mutual conversion between them is a common requirement. Below are several common arrays andStringMethods of conversion between:

1. Convert character array (char[]) and string (String) to each other

Character array to string

To convert a character array to a string, you can useStringClass constructor:

public class CharArrayToString {
    public static void main(String[] args) {
        char[] charArray = {'J', 'a', 'v', 'a'};
        
        // Convert character array to string        String str = new String(charArray);
        
        (str);  // Output: "Java"    }
}

String to character array

To convert a string to a character array, you can useStringClassictoCharArray()method:

public class StringToCharArray {
    public static void main(String[] args) {
        String str = "Java";
        
        // Convert string to character array        char[] charArray = ();
        
        // Output character array        for (char c : charArray) {
            (c + " ");  // Output: Java        }
    }
}

2. Convert string array (String[]) and string (String) to each other

String array to string

Can be used()Method, or use()Method converts a string array into a string.()Allows to specify connectors, and()Just convert the array into a comma-separated string.

import ;

public class StringArrayToString {
    public static void main(String[] args) {
        String[] strArray = {"Java", "Python", "C++"};
        
        // Use () to convert an array to a string, specify a separator        String joinedString = (", ", strArray);
        (joinedString);  // Output: "Java, Python, C++"        
        // Use () to convert to a string (including brackets and commas)        String arrayString = (strArray);
        (arrayString);  // Output: "[Java, Python, C++]"    }
}

String to string array

To convert a string into an array of strings, you can use()Methods are usually split according to a certain separator.

public class StringToStringArray {
    public static void main(String[] args) {
        String str = "Java,Python,C++";
        
        // Use split() method to split strings according to commas        String[] strArray = (",");
        
        // Output string array        for (String s : strArray) {
            (s);  // Output: Java, Python, C++        }
    }
}

3. Convert integer array (int[]) and string (String) to each other

Integer array to string

Can be used()Method converts an array of integers to a string, or useStringBuilderManually construct strings:

import ;

public class IntArrayToString {
    public static void main(String[] args) {
        int[] intArray = {1, 2, 3, 4, 5};
        
        // Use () to convert an array of integers to a string        String str = (intArray);
        (str);  // Output: "[1, 2, 3, 4, 5]"    }
}

String to integer array

Can be used()Method splits the string by delimiter and converts each part into an integer:

public class StringToIntArray {
    public static void main(String[] args) {
        String str = "1,2,3,4,5";
        
        // Use split() method to split strings        String[] strArray = (",");
        
        // Convert each string to an integer and save it into an array of integers        int[] intArray = new int[];
        for (int i = 0; i < ; i++) {
            intArray[i] = (strArray[i]);
        }
        
        // Output integer array        for (int num : intArray) {
            (num + " ");  // Output: 1 2 3 4 5        }
    }
}

4. Convert byte array (byte[]) and string (String) to each other

Byte array to string

Can be usedStringThe constructor of the class, converting the byte array into a string:

public class ByteArrayToString {
    public static void main(String[] args) throws Exception {
        byte[] byteArray = {74, 97, 118, 97};  // ASCII value corresponding to "Java"        
        // Convert byte array to string        String str = new String(byteArray, "UTF-8");
        
        (str);  // Output: "Java"    }
}

String to byte array

Can be usedStringClassicgetBytes()Method converts strings into byte arrays:

public class StringToByteArray {
    public static void main(String[] args) throws Exception {
        String str = "Java";
        
        // Convert string to byte array        byte[] byteArray = ("UTF-8");
        
        // Output byte array        for (byte b : byteArray) {
            (b + " ");  // Output byte value        }
    }
}

5. Convert integers (int) and strings (String) to each other

Integer to string

Can be used()or()method:

public class IntToString {
    public static void main(String[] args) {
        int num = 123;
        
        // Use () to convert integers to strings        String str1 = (num);
        
        // Use () to convert integers to strings        String str2 = (num);
        
        (str1);  // Output: "123"        (str2);  // Output: "123"    }
}

String to integer

Can be used()or()method:

public class StringToInt {
    public static void main(String[] args) {
        String str = "123";
        
        // Use () to convert a string to an integer        int num = (str);
        
        (num);  // Output: 123    }
}

Summarize

  • Character array and string transfer:usenew String(char[])ortoCharArray()
  • String array and string transfer:use()or()
  • Integer array and string:use()Or manually convert.
  • Byte array and string:usenew String(byte[])orgetBytes()
  • Integer and string:use()or()

These methods can help you efficiently convert arrays and strings in Java.

This is the end of this article about several common methods of converting arrays and Strings in Java. For more related contents of converting arrays and Strings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!