How to Convert a String to Array in Java
In this example, we are going to learn how to convert a string into an array in Java. We are going to discuss three different methods to break a string into an array.
- Using substring() method
- Using toCharArray() method
- Using split() method
1. Using substring() method
The string class provides substring() method to fetch part of a string. String class has two overloaded substring methods so here we use substring(int beginIndex, int endIndex)
to get one letter at a time and assign it to an array. Here is an example of a method :
public String[] copyToArray(String str) {
int length = str.length();
String[] array = new String[length];
/*
* This loop will break string one letter at a time a
*/
for(int a=0; a<length; a++) {
array[a] = str.substring(a, a+1);
}
return array;
}
input/output
value of str: hello returned array: ['h','e','l','l','o']
2. Using toCharArray() method
String class has toCharArray()
method to convert a string into a character array. The created character has the same length as the string has. Here is an example method that takes string input:
public char[] convertStringToArray(String str) {
/*
* This statements converts string into character array
*/
char[] array = str.toCharArray();
return array;
}
input/output
value of str: hello returned array: ['h','e','l','l','o']
3. Using split() method
The string class also has a split() method to break a string. There are two overloaded split methods, split(String regex) and split(String regex, int limit). Regex defines the regular expression into which string splits and “limit” defines the number of times the regular expression applied on the string. If the limit is set, then the returned array length is equal to the limit. Here are the examples:
3.1 Split by empty string
This method converts a string into an array using split("")
method, empty string parameter is used to split string in one character per array index.
public String[] splitInSingleCharacter(String str) {
String[] array = str.split("");
return array;
}
input/output
value of str: hello returned array: ['h','e','l','l','o']
3.2 Split by whitespace using regex
Here is another example that split string by space. In regex, ‘\\s’ is used for space:
public String[] splitBySpace(String str) {
String[] array = str.split("\\s");
return array;
}
input/output
value of str: This is john returned array: ['This','is','john']
3.3 Split by comma
In this example, string is split using comma.
public String[] splitByComma(String str) {
String[] array = str.split(",");
return array;
}
input/output
value of str: apple,mango,banana returned array: ['apple','mango','banana']
3.4 Split by comma and limit
In this example, split(String regex, int limit)
method applies regex to string n number of times.
private static String[] splitByCommaAndLength(String str, int limit) {
String[] array = str.split(",", limit);
return array;
}
input/output
value of str: apple,mango,banana,kiwi,grapes value of limit: 2 returned array: ['apple','mango,banana,kiwi,grapes']
You can now check our Array to String Java Example.
4. Download the source code
This is an example of converting a String to an array in Java.
You can download the full source code of this example here: How to Convert a String to Array in Java