Various Ways to Convert a Java String to a String Array Using split, Regex, Guava, and Groovy
This tutorial demonstrates multiple techniques for turning a Java String into a String[] array—including the basic split() method, a regular‑expression split, Guava utilities, and a concise Groovy approach—while providing complete code examples and output verification.
split() Method
The String API provides the split() method, which takes a delimiter and returns the split substrings as a String[] array. The example below passes an empty string to split() , resulting in each character becoming an element of the array.
public class FunTester {
public static void main(String[] args) {
String input = "FunTester";
String[] split = input.split("");
output(Arrays.asList(split));
}
}Console output shows each character indexed from 1 to 9, confirming that the array length matches the original string length.
INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.7
INFO-> 第1个:F
INFO-> 第2个:u
INFO-> 第3个:n
INFO-> 第4个:T
INFO-> 第5个:e
INFO-> 第6个:s
INFO-> 第7个:t
INFO-> 第5个:e
INFO-> 第9个:r
9
9Regular Expression Method
Using a regular expression can simplify string splitting for validation tasks such as email or phone number checks. The following code splits the string with the regex (?!^) , which inserts a split between every character.
public class FunTester {
public static void main(String[] args) {
String input = "FunTester";
String[] split = input.split("(?!^)");
output(Arrays.asList(split));
output(input.length());
output(split.length);
}
}The console output again lists each character and confirms that both the original string length and the array length are 9.
INFO-> 第1个:F
INFO-> 第2个:u
INFO-> 第3个:n
INFO-> 第4个:T
INFO-> 第5个:e
INFO-> 第6个:s
INFO-> 第7个:t
INFO-> 第5个:e
INFO-> 第9个:r
INFO-> 9
INFO-> 9Note: The split() method signature is public String[] split(String regex, int limit) ; the parameter name regex does not imply that the method always uses regular expressions.
Guava
The Guava library also offers utilities for converting strings to arrays. The process involves converting the string to a char[] with toCharArray() , turning that into a List via Chars.asList() , and finally using List.transform() together with toArray() to obtain a String[] .
public class FunTester {
public static void main(String[] args) {
String input = "FunTester";
String[] slist = Lists.transform(Chars.asList(input.toCharArray()), Functions.toStringFunction()).toArray(ArrayUtils.EMPTY_STRING_ARRAY);
output(Arrays.asList(slist));
output(input.length());
output(slist.length);
}
}The console output matches the previous examples, confirming the conversion.
Groovy
Groovy simplifies the conversion further by using the as operator. The same first line of Java code is retained, and the string is cast directly to a List .
String input = "FunTester";
def list = input as List
output(list)
output(list.size())
output(input.length())The as operator can be overridden, as discussed in a previous article on Groovy operator overloading.
Original content published by the FunTester public account; redistribution without permission is prohibited.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.