Explain Codes LogoExplain Codes Logo

String to string array conversion in java

java
string-manipulation
array-conversion
java-8-features
Nikita BarsukovbyNikita Barsukov·Mar 7, 2025
TLDR

To convert a String to a String array in Java, you can use the split() method.

String str = "one,two,three"; String[] arr = str.split(",");

After running this code, arr will now be an array with the elements {"one", "two", "three"}.

Breaking down strings

If you want to split a string into individual characters:

String str = "example"; String[] arr = str.split("");

This will give you an array {"e", "x", "a", "m", "p", "l", "e"}. Be aware that prior to Java 8, this would include an initial empty string.

The manual way

Here is a more manual approach, using a for loop to populate the array:

String str = "example"; String[] arr = new String[str.length()]; for(int i = 0; i < str.length(); i++) { // If it ain't broke don't fix it, but if it ain't a string, convert it. arr[i] = String.valueOf(str.charAt(i)); // arr is now ready to get down to business! }

This method allows much more control over how the strings are created.

Benefiting from libraries

Libraries like Guava can streamline this process for you:

String str = "example"; List<String> list = Lists.charactersOf(str); String[] arr = list.toArray(new String[0]);

Here, Guava takes the heavy lifting off your shoulders.

Streamlining with lambda expressions

Java 8 introduced Lambda expressions that can make your code more concise:

String str = "example"; String[] arr = str.chars() .mapToObj(c -> String.valueOf((char) c)) .toArray(String[]::new);

In this example, we're taking advantage of Java's fancy streams to create an array of strings in just a few lines.

Regular expressions with split()

For a more fine-grained control, use regular expressions with split():

String str = "Java 11 & Java 8"; String[] arr = str.split("\\s+&\\s+"); // What's that? A regex split? Indeed!

The resulting array after splitting by & with surrounding spaces is {"Java 11", "Java 8"}.

Consider method visibility

When you implement these methods, be sure to consider method visibility:

public String[] stringToArray(String str) { // Knock knock. Who's there? A public method that can be accessed anywhere! return str.split(","); }

The public modifier here means this method can be called from any class in your application.

Advanced topics

Splitting without using split()

What if split() doesn't work for your case? Resort to manual handling:

String str = "12345"; String[] arr = IntStream.range(0, str.length()) .mapToObj(i -> String.valueOf(str.charAt(i))) // Work that loop, manual handling style! .toArray(String[]::new);

Dealing with special characters

Careful with special characters in regex:

String str = "one.two.three"; // We're going on a dot hunt, gonna catch a big one! String[] arr = str.split("\\.");

Since . is a special character in regex (it means "any character"), the dot needs to be escaped.

Handling empty strings

Processing empty strings requires precise handling:

String str = ""; String[] arr = str.split(","); if (arr.length == 1 && arr[0].isEmpty()) { // Handling empty strings here - less exciting than I imagined. }

Here, splitting an empty string results in an array containing a single empty string, which you may want to handle differently.