java中如何讲一个字符串数组中的数字取出来.
答案:6 悬赏:80 手机版
解决时间 2021-02-21 11:20
- 提问者网友:锁深秋
- 2021-02-20 15:19
其中里面含有数字如:"100”,"202","abcd","%^&","int"等等很多诸如此类的字符串,用循环怎么帅选出其中的数字,并转化为整数。
最佳答案
- 五星知识达人网友:有你哪都是故乡
- 2021-02-20 16:22
我不知道有没有更好的办法,我一下想到的是这个
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);
}
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楼网友:胯下狙击手
- 2021-02-20 20:11
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楼网友:野慌
- 2021-02-20 19:19
用正则表达式
- 3楼网友:酒醒三更
- 2021-02-20 18:52
使用正规表达式,不难
~
~
~
- 4楼网友:拾荒鲤
- 2021-02-20 18:13
遍历 每个字符 1-9 取出来不久好了
- 5楼网友:神也偏爱
- 2021-02-20 17:57
java中定义一个字符串数组方式如下:
1.string[] str={"aaa","bbb","ccc"};
2.string str[]={"aaa","bbb","ccc"};
string类型和其他基本类型相似,创建数组时,有上述两种方式。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |