In this tutorial, you will learn how to convert a String to a String Array in Java. There are two approaches you can use depending on the output desired.

The first approach is to convert the individual sequence of characters to a String Array. The second approach will use the full sequence of characters for the Array.

Use the split() method

Copy and paste the following code into the Main.java file of your Java project.

package com.javawhizz;

public class Main {
    public static void main(String[] args) {
        String[] stringArray = "JavaWhizz".split("(?!^)");

        for (int i = 0; i < stringArray.length; i++) {
            System.out.println(i +" = "+stringArray[i]);
        }

    }
}

The split() method uses a regular expression to split the current String. You should pass the regular expression as the argument of the method.

In this example, use the regular expression "(?!^)". This regular expression matches empty strings. As a result, matching of the beginning of the String does not happen to avoid getting a leading empty String.

With this in place, the split() method will split the sequence of characters into a String Array.

Output

0 = J
1 = a
2 = v
3 = a
4 = W
5 = h
6 = i
7 = z
8 = z

Use Java varargs

Copy and paste the following code into the Main.java file.

package com.javawhizz;

public class Main {
    private static String[] stringToArray(String... strings){
        return strings;
    }
    
    public static void main(String[] args) {
        
        String[] stringToArray = stringToArray("JavaWhizz");
        for (int i = 0; i < stringToArray.length; i++) {
            System.out.println(i + " = "+stringToArray[i]);
        }
    }
}

The Java varargs is a mechanism to pass an infinite number of arguments. Behind the scenes, varargs gets converted to an Array.

Nonetheless, the Array does not contain the individual sequence of characters. The strings you pass to as the argument of the varargs form the elements of the Array.

For example, the String "JavaWhizz" is not split into individual characters. In this case, the String forms the first element of the Array.

Output

0 = JavaWhizz

An alternative to this approach is the manual addition of the String to an Array. Let’s look at an example.

Copy and paste the following code into the Main.java file.

package com.javawhizz;

public class Main {
    public static void main(String[] args) {
        String name = "JavaWhizz";

        String[] strings = {name};

        for (int i = 0; i < strings.length; i++) {
            System.out.println(i+" = "+strings[i]);
        }
    }
}

This code defines a variable that contains the String "JavaWhizz". To create a String Array, create a new Array and add the String variable to the Array.

Note that this does not add the individual sequence of characters to the Array but the full String. As, a result this example has the same output as the previous one and you can use either of them.

Output

0 = JavaWhizz

Conclusion

In this tutorial, you have learned how to convert a String to a String Array in Java. The approaches covered in this tutorial include using the split() method, Java varargs, and manual addition of the String to an Array. In the next tutorial, you will learn how to convert a String Array to a String in Java.

Happy hacking!


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *