Learn how to convert string to array in Java.
In Java, a String is an object that represents the sequence of characters. To use Strings, we must first import the String class from the java.lang package. The String array is a collection of strings with a fixed length. Since arrays are immutable[cannot grow], Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created. In this Article we will discuss : We are discussing the conversion of string to array in Java, not strings or arrays specifically. Strings are Objects in Java that are internally supported by a char array. Strings
are immutable because arrays are immutable [they can't grow]. Every time you make a change to a String, a new String is produced. Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. Array is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array. The first element of an array is stored at the
0th0^{th} index, the second element is stored at the 1st1^{st} index, and so on. There are two different ways to covert String[] to Char[ ] in Java:Overview
Scope
Introduction
Convert a 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.
- Get the string.
- Make a character array that is the same length as string.
- Copy the character at the ithi^{th} position of the string to the ith i^{th} index of the array by traversing the string.
- Return or perform the operation on the character array.
Using the Naive Approach, convert a String to a Character array in Java.
import java.util.*; public class MyClass { public static void main[String args[]] { String str = "Scaler"; // Given String char[] arr = new char[str.length[]]; // Creating array of string length // Copy character by character into array for [int i = 0; i < str.length[]; i++] { arr[i] = str.charAt[i]; } // Printing the character array for [char x : arr] { System.out.println[x]; } } }
Output:
2. Using toCharArray[] Method
To convert a string to a char array, use the toCharArray[] function.
- Get the string.
- Make a character array that is the same length as string.
- Store the array return by toCharArray[] method.
- Return or perform operation on character array.
Using the toCharArray[] Method, convert a String to a Character array in Java.
import java.util.*; public class MyClass { public static void main[String args[]] { String str = "Scaler"; // Creating array of string and Store the array return by toCharArray[] method. char[] arr = str.toCharArray[]; // Printing the character array for [char x : arr] { System.out.println[x]; } } }
Output:
Converting String to String Array in Java
We'll learn how to convert a string to array in Java in this section.
In Java, there are four ways to convert a String to a String array:
- Using String.split[] Method
- Using Pattern.split[] Method
- Using String[ ] Approach
- Using toArray[] Method
Using String.split[] Method
The String.split[] 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 array in Java using the String.split[] method.
public class SplitMethod { public static void main[String[] args] { //declaring and initializing a string String str = "Using the split[] method to convert a string to array in Java "; String[] arr = null; //declaring an empty string array //converting using String.split[] method with whitespace as a delimiter arr = str.split[" "]; //printing the converted string array for [int i = 0; i< arr.length; i++] { System.out.println[arr[i]]; } } }
Output:
Using the Pattern.split[] method to convert a string to array in Java
Example 2: We changed the string to array in Java in the following example using the delimiter, which is a , [comma].
public class SplitMethod { public static void main[String[] args] { //declaring and initializing a string String str = "What,is,your,name ?"; String[] arr = null; //declaring an empty string array //converting using String.split[] method with comma[,] as a delimiter arr = str.split[","]; //printing the converted string array for [int i = 0; i< arr.length; i++] { System.out.println[arr[i]]; } } }
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 must first import the Pattern class into our Java code as follows:
import java.util.regex.Pattern;
Consider the following example, in which we will split a string into an array by using whitespace as the delimiter.
//importing Pattern class import java.util.regex.Pattern; public class PatternSpiltmethod { public static void main[String[] args] { //declaring and initializing a string String str = "Using the Pattern.split[] method to convert a string to array in Java "; //declaring an empty string array String[] arr = null; //parsing white space as a parameter Pattern ptr = Pattern.compile[" "]; //storing the string elements in array after splitting arr = ptr.split[str]; //printing the converted string array for [int i = 0; i< arr.length; i++] { System.out.println[arr[i]]; } } }
Output:
Using the Pattern.split[] method to convert a string to array in Java
Example 2: We may also use any string or pattern as a delimiter to break a string into an array. We've used the * delimiter here.
//importing Pattern class import java.util.regex.Pattern; public class PatternSpiltmethod { public static void main[String[] args] { //declaring and initializing a string with a separator String str = "What&qais&qayour&qaname ?"; //declaring an empty string array String[] strArray = null; //splitting the string with delimiter as #a1 String patternStr = "&qa"; Pattern ptr = Pattern.compile[patternStr]; //storing the string elements in array after splitting strArray = ptr.split[str]; //printing the converted string array for [int i = 0; i< strArray.length; i++] { System.out.println[strArray[i]]; } } }
Output:
Using String[ ] Approach
We can simply convert string to string array by using the string index [ ]. Here, we pass the string to String [ ] {}.
Consider the following example, which shows how to convert a string to array in Java using the String[ ] function.
import java.util.Arrays; public class StringtoStringArray { public static void main[String[] args] { //declaring and initializing a string String str = "Using the String[] method to convert a string to array in Java"; //passing the string to String[] {} String[] arr = new String[] {str}; //printing the string array using Arrays.toString[] System.out.println[Arrays.toString[arr]]; } }
Output
[Using the String[] method to convert a string to array in Java]
Using toArray[] Method
The toArray[] function of the List class can also be used to convert a string to array in Java. It takes a list of type String as the input and converts each entity into a string array.
Consider the following example where we have converted the list of strings into a string array.
//importing ArrayList and List class import java.util.ArrayList; import java.util.List; public class StringListtoArray { public static void main[String[] args] { //creating a list of type string List list = new ArrayList []; //adding elements to list list.add["What"]; list.add["is"]; list.add["your"]; list.add["name ?"]; //size of list int list_size = list.size[]; //creating string array String[] arr = new String[list_size]; //converting to string array list.toArray[arr]; //printing the string array for[int i = 0; i < arr.length; i++] { System.out.println[arr[i]]; } } }
Output
Conclusion
- In Java, the string is basically an object that represents a sequence of char values. An array of characters works the same as Java string.
- A Java array is an object which contains elements of a similar data type.
- We use the standard iterative method to convert string to char array in java.
- We also learned how to convert string to char array in java by using toCharArray[] method.
- We learned how to convert string to array in java using 4 different methods.