You could do this:
String str = "...";
List<String> elephantList = Arrays.asList(str.split(", "));
In Java 9+, create an unmodifiable list with List.of.
List<String> elephantList = List.of(str.split(", ")); // Unmodifiable list.
Basically the .split() method will split the string according to (in this case) delimiter you are passing and will return an array of strings.
However, you seem to be after a List of Strings rather than an array. So the array must be turned into a list. We can turn that array into a List object by calling either Arrays.asList() or List.of.
FYI you could also do something like so:
String str = "...";
ArrayList<String> elephantList = new ArrayList<>(Arrays.asList(str.split(","));
But it is usually better practice to program to an interface rather than to an actual concrete implementation. So I would not recommend this approach.
Answer from npinti on Stack OverflowYou could do this:
String str = "...";
List<String> elephantList = Arrays.asList(str.split(", "));
In Java 9+, create an unmodifiable list with List.of.
List<String> elephantList = List.of(str.split(", ")); // Unmodifiable list.
Basically the .split() method will split the string according to (in this case) delimiter you are passing and will return an array of strings.
However, you seem to be after a List of Strings rather than an array. So the array must be turned into a list. We can turn that array into a List object by calling either Arrays.asList() or List.of.
FYI you could also do something like so:
String str = "...";
ArrayList<String> elephantList = new ArrayList<>(Arrays.asList(str.split(","));
But it is usually better practice to program to an interface rather than to an actual concrete implementation. So I would not recommend this approach.
Well, you want to split, right?
String animals = "dog, cat, bear, elephant, giraffe";
String[] animalsArray = animals.split(",");
If you want to additionally get rid of whitespaces around items:
String[] animalsArray = animals.split("\\s*,\\s*");
In Java, how would I structure my regex to do a string split but only on commas, or commas followed by whitespace?
performance - Quickest way to split a delimited String in Java - Software Engineering Stack Exchange
java - Split a string with arbitrary number of commas and spaces - Stack Overflow
Splitting Strings on Comma With Embedded Commas
Videos
**And round braces
Basically I have the following string split, but it breaks for multi word names. This is an example of the structure:
Sao Paulo, (-23.55, -46.63)
Where sArr is a string array, I attempted to match commas, whitespace and brackets, but didn't account for names with spaces.
sArr = line.split(("[\\s,(|)]+")); How could I match only commas, or commas with a whitespace, so that "Sao Paulo" doesn't turn into "Sao","Paulo"?
I've written a quick and dirty benchmark test for this. It compares 7 different methods, some of which require specific knowledge of the data being split.
For basic general purpose splitting, Guava Splitter is 3.5x faster than String#split() and I'd recommend using that. Stringtokenizer is slightly faster than that and splitting yourself with indexOf is twice as fast as again.
For the code and more info see https://web.archive.org/web/20210613074234/http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance (original link is dead and corresponding site does not appear to exist anymore)
As @Tom writes, an indexOf type approach is faster than String.split(), since the latter deals with regular expressions and has a lot of extra overhead for them.
However, one algorithm change that might give you a super speedup. Assuming that this Comparator is going to be used to sort your ~100,000 Strings, do not write the Comparator<String>. Because, in the course of your sort, the same String will likely be compared multiple times, so you will split it multiple times, etc...
Split all the Strings once into String[]s, and have a Comparator<String[]> sort the String[]. Then, at the end, you can combine them all together.
Alternatively, you could also use a Map to cache the String -> String[] or vice versa. e.g. (sketchy) Also note, you are trading memory for speed, hope you have lotsa RAM
HashMap<String, String[]> cache = new HashMap();
int compare(String s1, String s2) {
String[] cached1 = cache.get(s1);
if (cached1 == null) {
cached1 = mySuperSplitter(s1):
cache.put(s1, cached1);
}
String[] cached2 = cache.get(s2);
if (cached2 == null) {
cached2 = mySuperSplitter(s2):
cache.put(s2, cached2);
}
return compareAsArrays(cached1, cached2); // real comparison done here
}
You need two steps, but only one line:
String[] values = input.replaceAll("^[,\\s]+", "").split("[,\\s]+");
The call to replaceAll() removes leading separators.
The split is done on any number of separators.
The behaviour of split() means that a trailing blank value is ignored, so no need to trim trailing separators before splitting.
Here's a test:
public static void main(String[] args) throws Exception {
String input = ",A,B,C,D, ,,,";
String[] values = input.replaceAll("^[,\\s]+", "").split("[,\\s]+");
System.out.println(Arrays.toString(values));
}
Output:
[A, B, C, D]
You do not only want to include the next few whitespaces into your match, but also the consecutive commata to split on them as one unit:
(,\s*)+
current.split("(?:,\\s*)+")
I have a sting which I want to split on comma. However, some of the terms that have been joined to create the string have a comma within them which I do not wish to split on. As an example, the string format I am dealing with follows:
'Cat1,Cat2, Big,Cat3, Cat4, Small,Cat5' -> 'Cat1', 'Cat2, Big', 'Cat3', 'Cat4, Small', 'Cat5'.
I tried using regex to separate where we have a comma followed by an uppercase character but could not seem to get it working. A standard comma when splitting would incorrectly split Cat3, Big into 'Cat3', 'Big'. Does anyone have an ideas to overcome this issue?