java中如何讲一个字符串数组中的数字取出来.

其中里面含有数字如:"100”,"202","abcd","%^&","int"等等很多诸如此类的字符串,用循环怎么帅选出其中的数字,并转化为整数。

我不知道有没有更好的办法,我一下想到的是这个

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] strs=new String[]{"100","202","abcd","%^&","int"};
        List<Integer> ints=new ArrayList<>();
        for(String str:strs){
            Integer num=null;
            try {
                num=Integer.parseInt(str);
            } catch (NumberFormatException e) {
                
            }
            if(num!=null){
                ints.add(num);
            }
        }
        System.out.println(ints);
    }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-23
public static void main(String[] args) {
String a="uuu034x3rews432uo428lj4uo24";
String regex="\\d+";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(a);
while(m.find()){
int aint=Integer.valueOf(m.group());
System.out.println("数:"+aint);
}
}追问

我说的是在字符串数组里找的,不是字符串里面???

追答

数组循环都不会么??????就这态度就不再回答了。

追问

楼主我错了。呜呜。。。。。。。。。。。。

第2个回答  2017-09-30
使用正规表达式,不难

~
~
~
第3个回答  2015-09-22
遍历 每个字符 1-9 取出来不久好了
第4个回答  2011-10-23
用正则表达式
相似回答