import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class get_StringNum {
/**
*2012.6.2
*/
public static void main(String[] args) {
String a="love23next234csdn3423javaeye";
List<String> digitList = new ArrayList<String>();
Pattern p = Pattern.compile("[^0-9]");
Matcher m = p.matcher(a);
String result = m.replaceAll("");
for (int i = 0; i < result.length(); i++) {
digitList.add(result.substring(i, i+1));
}
System.out.println(digitList);
}
}
output:
[2, 3, 2, 3, 4, 3, 4, 2, 3]
相同的思路:
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class get_StringNum {
/**
*2012.6.2
*/
public static void main(String[] args) {
String a="love23next234csdn3423javaeye";
List<String> ss = new ArrayList<String>();
for(String sss:s.replaceAll("[^0-9]", ",").split(",")){
if (sss.length()>0)
ss.add(sss);
}
System.out.print(ss);