How to Convert a String to String Array in Java?

8 May 2025 | 6 min read

In Java, a String is an object that represents the sequence of characters. In order to use Strings, we need to import the String class defined in java.lang package. The String array is an array of strings with a fixed length. In this section, we will learn how to convert String into String array.

String: In Java, a string is an object that represents a sequence of characters. Strings are immutable, meaning their values cannot be changed once they are created. The String class in the java.lang package provides various methods for string manipulation.

String Array: A string array is an array of strings, where each element of the array holds a string value. It allows developers to store multiple strings in a single data structure.

Converting String to String Array

There are four ways to convert a String into String array in Java:

  1. Using String.split() Method
  2. Using Pattern.split() Method
  3. Using String[ ] Approach
  4. Using toArray() Method

Using String.split() Method

The String.split() method is used to split the string into individual strings entities based on the given delimiter (whitespace or other symbols). We can store these entities directly in a string array.

Syntax

Let's consider the following example where we use String.split() method to convert a string into a string array.

TestSplitMethod.java

Output:

How to convert String to String array in Java

Explanation

The Java code that is provided shows how to use the split() function to turn a string into a string array with whitespace acting as the delimiter. It uses the split() method to divide a string variable called str into distinct words depending on whitespace after initializing it with a specific sentence. A for loop is used to print the substrings that are produced to the console after they have been saved in a string array called strArray. This indicates that the string has been successfully divided into distinct substrings based on whitespace since it enables each word from the original string to be displayed on a different line.

Example 2:

In the following example, we have converted the string to a string array based on the delimiter that is a , (comma).

TestSplitMethod2.java

Output:

How to convert String to String array in Java

Explanation

The Java code snippet that is provided defines a class called TestSplitMethod2, which has a main() function. In this procedure, "Hello,have,a,nice,day" is used to initialise the string variable commaSeparatedStr. The string is divided into separate substrings using the split() method with a comma as the delimiter. The substrings are then saved in a string array called strArray. Each substring in strArray is sent to the console as it is iterated through using a for loop. Consequently, every word from the initial string separated by commas appears on a different line.

Using Pattern.split() Method

The Pattern.split() method is used to split the string into an array of strings with the help of regular expression (pattern) as the delimiter.

In order to use the method, we need to import the Pattern class in our Java code as:

Syntax

Let's consider the following example where we split a string into an array by using the delimiter as whitespace.

SplitMethodOfPatternClass.java

Output:

How to convert String to String array in Java

Explanation

The Java code snippet that is provided defines a class called TestSplitMethod2, which has a main() function. In this procedure, "Hello,have,a,nice,day" is used to initialise the string variable commaSeparatedStr. The string is divided into separate substrings using the split() method with a comma as the delimiter. The substrings are then saved in a string array called strArray. Each substring in strArray is sent to the console as it is iterated through using a for loop. Consequently, every word from the initial string separated by commas appears on a different line.

Example 2:

We can also split a string into an array by using any string or pattern as a delimiter. Here, we have used the delimiter #a1.

SplitMethodOfPatternClass2.java

Output:

How to convert String to String array in Java

Explanation

This snippet of Java code divides a string str into separate substrings according to the delimiter #a1 by using the Pattern.split() method. The code initialises the string str with the value "Hello #a1Ben #a1how #a1are #a1you?" after importing the Pattern class. A pattern object, ptr, is constructed using Pattern, and an empty string array, strArray, is declared.utilise the delimiter pattern #a1 in compile(). Next, ptr is subjected to the split() method, which divides str into substrings according to #a1 and stores the outcome in strArray.

The above example splits the single string into five separate strings based on the delimiter #a1. The parts of the string that matched the delimiter are not included in the array.

Using String[ ] Approach

We can simply convert string to string array by using the string index [ ]. Here, we pass the string to String [ ] {}.

Syntax

Consider the following example where we have used the String[] to convert a string into a string array.

StrToStrArray.java

Output:

How to convert String to String array in Java

Explanation

"Converting string to string array using String[]" is the value that this Java program initializes in the string variable str. The string str is the only element in the string array strArray that is created at that point. Arrays.toString(), which turns the array into a string representation, is then used to print the contents of the string array to the console.

Using toArray() Method

We can also convert String to String array by using the toArray() method of the List class. It takes a list of type String as the input and converts each entity into a string array.

Syntax

Consider the following example where we have converted the list of strings into a string array.

StringListtoArray.java

Output:

How to convert String to String array in Java

Explanation

The ArrayList and List classes are imported first in this Java programme from the java.util package. Next, an ArrayList called list is created to hold strings. Four strings, "Hello", "Welcome", "To", and "Tutorial", are added to the list. The list's size is then calculated using size(), and a string array called strArray is initialised with the same size as the list. Next, it uses the toArray() method to transform the contents of the list to a string array.