2如何提取字符串中的数字,放到ArrayList中?import java.util.Date;
public class Demo1 {public ArrayList fun(String s)
{
//提取字符串中的数字,放到ArrayList中
//...?
} public static void main(String[] args) {
String s="h5ello56,wor88ld";
Demo1 demo1=new Demo1();
ArrayList a=demo1.fun(s);
//?在此打印输出a

}
}

解决方案 »

  1.   


        public static List fun(String abc)
        {
            List list = new ArrayList();
            if(abc != null)
            {
                char[] cha = abc.toCharArray();
                for(int i = 0; i < cha.length; i++)
                {
                    list.add(String.valueOf(cha[i]));
                }
            }
            return list;
        }
      

  2.   

    漏看了只需要数字...
    再加两行
        public static List fun(String abc)
        {
            List list = new ArrayList();
            if(abc != null)
            {
                char[] cha = abc.toCharArray();
                for(int i = 0; i < cha.length; i++)
                {
                    String str = String.valueOf(cha[i]);
                    try
                    {
                        Integer.parseInt(str);
                        list.add(str);
                    }
                    catch(NumberFormatException ex)
                    {
                        
                    }
                }
            }
            return list;
        }
      

  3.   

    String a = "aa3345vvv56,6f56gh34";
    String[] wa = a.split("[a-zA-Z,]+"); for (int i = 0; i < wa.length; ++i) {
    System.out.println(wa[i]);
    }
      

  4.   

    不好意思我我没说清楚,我现在要的是如:
    String s="h5ello56,wor88ld";
    则:
    得到 5
        56
        88favorite7w ,再帮我一下啊 ,,
      

  5.   

    谢谢  Ray.Z,
    谢谢  甘草  ,正则表达式超强的啊,
    不过,现在不用正则表达式,该怎么写啊?
      

  6.   

    不用正则的话,只能写很钝的方法了:
        public static void main(String[] args) {        String s = "1asd234sd12332bg46,54as78a,sd23,9a.09.a";
            String s2 = "0123456789";
            String charTemp;
            String strTemp = "";
            List list = new ArrayList();        for (int i = 0; i < s.length(); i++) {            charTemp = String.valueOf(s.charAt(i));
                int index = s2.indexOf(charTemp);            if (index >= 0) {
                    strTemp = strTemp + charTemp;
                    if (i == (s.length() - 1)) {
                        list.add(strTemp);
                    }
                } else {
                    if (!"".equals(strTemp)) {
                        list.add(strTemp);
                        strTemp = "";
                    }
                }
            }        for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).toString());
            }    }
      

  7.   

    1. 正则表达式做的: public static ArrayList fun(String s) {
    String[] result = s.split("[(a-zA-z)*]");
    ArrayList a = new ArrayList();
    for (String s1 : result)
    if(!s1.equals("")){
    a.add(s1);
    }
    return a;
    }2 判断字符串里面内容是否是在a-zA-z之间,若是,则加空格。若否则读取数字
    再将其分段
    [code=Java]
    public static ArrayList fun(String s) {
    ArrayList a = new ArrayList();
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<s.length();i++ ){
    if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')){
    sb.append("  ");
    }else{
    sb.append(s.charAt(i));
    }
    }
    StringTokenizer st=new StringTokenizer(sb.toString());
    while(st.hasMoreTokens()){
    a.add(st.nextToken());
    }
    return a;
    }
    [code]