java从任意字符串中截取数字2
发布网友
发布时间:2024-01-24 21:09
我来回答
共3个回答
热心网友
时间:2024-02-15 22:58
String s = "asd546%ss55asd";
String s1 =s.replaceAll("\\D+", "");
List aa = new ArrayList();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
sb.append(c);
if(i == 2){
sb.append(",");
continue;
}
}
System.out.println(sb.toString());
你好,亲自写的,测试通过,请采纳追答请采纳
热心网友
时间:2024-02-15 22:58
递归遍历字符串,判断是不是数字,不是的话统一转成字符a,
递归替换aa为a
最后就是a546a55,根据a分解存入list(算不算用list了)
循环输出
热心网友
时间:2024-02-15 22:59
public static void main(String[] args) {
String s = "asd546%ss55asd";
StringBuffer result = new StringBuffer();
int tempi = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
if (i == tempi + 1 || tempi == 0) {
result = result.append(s.charAt(i));
} else {
result = result.append("," + s.charAt(i));
}
tempi = i;
}
}
List<String> list=Arrays.asList((result.toString().split(",")));
for (String object : list) {
System.out.println(object);
}
}