Possible Duplicate:
Java: How to convert comma separated String to ArrayList
I have a the String Red*Blue*Yellow*Green*White
. How do I break that String out by *
into a List<String>
?
Possible Duplicate:
Java: How to convert comma separated String to ArrayList
I have a the String Red*Blue*Yellow*Green*White
. How do I break that String out by *
into a List<String>
?
You can try this: -
String str = "Red*Blue*Yellow*Green";
String[] arr = str.split("\\*");
List<String> list = new ArrayList<String>(Arrays.asList(arr));
NOTE:-
Arrays.asList
returns you an unmodifiable
list, so if you want a modifiable list
, you need to create a new list by using the constructor of ArrayList
, that takes a Collection
object as parameter.
Also, since *
is a special character in Regex
, and String.split()
takes a Regex
for splitting. So, you need to escape the *
with a backslash.
OUTPUT: -
[Red, Blue, Yellow, Green]
String[] str ="Red*Blue*Yellow*Green*White".split("\\*");
List<String> list = Arrays.asList(str);
Output:
[Red, Blue, Yellow, Green, White]
Please try this
String ss="Red*Blue*Yellow*Green*Whit";
String sss[] = ss.split("\\*");
List <String> ssss = Arrays.asList(sss);