Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to split a string base on delimiter : and <space> Below are my desired output.

input: "1:123 456"
and i wanna make output like this: "123","456" into arraylist.

and here is my failed code:

Java
List<String> skill = new ArrayList<String>();
		List<String> result = new ArrayList<String>();
		skill.add("1:123 456");
		
		for (String st : skill) {
			String[] array = st.split("[:]");
			result.add(array[1]);
		}
		System.out.println(result);


please help me... thanks in advance.
Posted
Comments
PIEBALDconsult 6-Aug-15 23:51pm    
Doesn't Java support Regular Expressions?

1 solution

Java
for (String st : skill) {
	String[] array = st.split(":");
	result.Add(array[1].split("\\s+"));
}
 
Share this answer
 
v2
Comments
EADever 7-Aug-15 2:13am    
i got this when apply your code: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
DamithSL 7-Aug-15 2:16am    
use \\s+
DamithSL 7-Aug-15 2:16am    
there is double \ in the above code but it is rendered as one \ :)
EADever 7-Aug-15 2:25am    
i got it... but main problem is here: The method AddRange(String[]) is undefined for the type List<string>
DamithSL 7-Aug-15 2:35am    
result.Add(array[1].split("\\s+"));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900