Convert String to Array in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Strings in Java are objects which represent a sequence of characters. Java Strings are immutable, which implies their value cannot be changed once they are created.

We can use a simple for loop or toCharArray() method to convert string to char array.

String array in Java is an array of Strings. Arrays in Java are of fixed length. We can convert a String to an array using any one of the following ways: String.split(), Pattern.split(), String[] {}, and toArray().

Introduction

Strings in Java are objects of String class which are nothing but a sequence of characters. Strings are immutable in Java, which implies when we modify the value of a String, a new String object is created instead of modifying the value of the existing String object. We can import String class in Java from the java.lang package.

In Java, an array is essentially an object that holds elements of the same data type. Furthermore, these elements are stored sequentially in memory, occupying contiguous memory locations.. The size of arrays cannot be changed in Java which implies an array is a set of fixed number of elements.

In this article, we will go through character and string arrays in Java.

:::

Convert a String to Character Array in Java

There are two different ways to covert a String object to a character array in Java:

String to character array in Java

1. Naive Approach

A simple method is to read all of the characters in the string and assign them one by one to a Character array using a standard for-loop.

Detailed Procedure:

  1. Get the string.
  2. Create a character array that is the same length as the string.
  3. Copy the character at the ithi^{th} position of the string to the ithi^{th} index of the array by traversing the string.
  4. Return or perform the operation on the character array.

Code:

Output:

2. Using toCharArray() Method

To convert a string to a char array, we can use the toCharArray() function.

Detailed Procedure:

  1. Get the string.
  2. Call the toCharArray() method and store the character array returned by it in a character array.
  3. Return or perform operation on the character array.

Code:

Output:

:::

Converting String to String Array in Java

We'll learn how to convert a string to an array of strings in Java in this section.

In Java, there are four methods to convert a String to a String array:

  • Using The String.split() Method
  • Using The Pattern.split() Method
  • Using The String[ ] Method
  • Using The toArray() Method

Using String.split() Method

This method is used to split a string into distinct strings based on the delimiter provided (whitespace or other symbols). These entities can be directly stored in a string array.

Consider the following example, which shows how to convert a string to an array in Java using the String.split() method.

Output:

Example 2: We changed the string to array in Java in the following example using the # delimiter.

Output:

Using Pattern.split() Method

The Pattern.split() method uses a regular expression(pattern) as the delimiter to split a string into an array of strings.

To use the technique, we have to import the Pattern class into our Java code as shown in the code below. This Pattern class can compile complex regex expressions.

Consider the following example, in which we will split a string into an array by using whitespace as the delimiter.

Output:

Example 2: We may also use any string or pattern as a delimiter to break a string into an array. We've used the &d& delimiter here.

Output:

Using String[ ] Approach

We can convert a string to a string array by simply passing the string in the curly brackets of String [] {}. The impact of this conversion is that a String array will be created containing a single element - the input string itself.

Consider the following example, which shows how to convert a string to an array in Java using the String[] {} approach.

Output

Using toArray() Method

The toArray() function of the List class can also be used to convert a string to array in Java. It accepts a list of type String as the input and converts each entity into an element of a string array.

Output

Conclusion

  • In Java, the string is a sequence of char values. An array of characters works the same as a Java string.
  • Strings are immutable in Java.
  • A Java array is an object which contains elements of a similar data type. Arrays in Java are of fixed length. *For converting a String into a character array using either a naive for loop or the toCharArray() method.
  • For converting a string into a string array based on a delimeter, we can use String.split() or Pattern.split() methods.
  • We can also convert a string to an array containing the string as the only element by passing the string inside the curly brackets in String[] {} approach.
  • To convert a list of strings in Java to a string array, we can use the toArray() method of List class.