2如何提取字符串中的数字,放到ArrayList中?

lord_is_layuping 2008-02-23 04:32:43
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

}
}
...全文
168 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
rain_night 2008-02-23
  • 打赏
  • 举报
回复
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]
kkkdyc 2008-02-23
  • 打赏
  • 举报
回复
补输出结果
1
234
12332
46
54
78
23
9
09
kkkdyc 2008-02-23
  • 打赏
  • 举报
回复
不用正则的话,只能写很钝的方法了:
    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());
}

}
favorite7w 2008-02-23
  • 打赏
  • 举报
回复
原来是要连续数字的啊~```
那还是用正则式最简洁方便,其它方法写起来太复杂了....
lord_is_layuping 2008-02-23
  • 打赏
  • 举报
回复
谢谢 Ray.Z,
谢谢 甘草 ,正则表达式超强的啊,


不过,现在不用正则表达式,该怎么写啊?
wenzheng38 2008-02-23
  • 打赏
  • 举报
回复
上面提取到的都是单个的数字
应该也可是连续的数字
最好是写个正则表达式来获取字符串中的数字
lord_is_layuping 2008-02-23
  • 打赏
  • 举报
回复
不好意思我我没说清楚,我现在要的是如:
String s="h5ello56,wor88ld";
则:
得到 5
56
88

favorite7w ,再帮我一下啊 ,,


healer_kx 2008-02-23
  • 打赏
  • 举报
回复
String a = "aa3345vvv56,6f56gh34";


String[] wa = a.split("[a-zA-Z,]+");

for (int i = 0; i < wa.length; ++i) {
System.out.println(wa[i]);
}

healer_kx 2008-02-23
  • 打赏
  • 举报
回复
你可以用正则.
favorite7w 2008-02-23
  • 打赏
  • 举报
回复
漏看了只需要数字...
再加两行


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;
}
favorite7w 2008-02-23
  • 打赏
  • 举报
回复

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;
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧