Home » Core Java » How to Convert a String to Array in Java

About Sadia Sher

This is Sadia (Backend Developer-Java). I have graduated from Jinnah University For Women, Karachi, Pakistan. Coding is my first love, therefore I am a developer. Why Java? Because It has been found as the most diverse language with plenty of APIs, frameworks and integration support. Other than this I am a Yogi. Ever since I practice yoga my lifestyle changes to morning bird. This strengthens my core just as JVM strengthens Java.

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.

String to Array Java
  1. Using substring() method
  2. Using toCharArray() method
  3. 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 that takes a string as input and breaks it into an array:

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 array 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.

Download
You can download the full source code of this example here: How to Convert a String to Array in Java

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments