Topic: 怎么把string中的数字读出来?

  Print this page

1.怎么把string中的数字读出来? Copy to clipboard
Posted by: Butterfly_125
Posted on: 2005-07-24 16:27

问个很菜的问题:(自己就是一直没有想出来)
比如说有:String s = ("adfasd12342ds");
怎样才可以只读出其中的数字?

2.Re:怎么把string中的数字读出来? [Re: Butterfly_125] Copy to clipboard
Posted by: why
Posted on: 2005-07-24 21:44

Regular Expression
java.util.regex
http://javaalmanac.com/egs/java.util.regex/GroupInPat.html
pattern for the number is: \d+

3.Re:怎么把string中的数字读出来? [Re: Butterfly_125] Copy to clipboard
Posted by: 1047
Posted on: 2005-08-05 10:21

这得一个一个的处理了吧
除非是全是数字的可以用函数来直接转换

char temp[N];
int res=0, len=0;
for(int i=0;i<strlenMoon;i++)
{
char c=s.charAtLight Bulb;
if(c>='0'&&c<='9')
temp[len++]=c-'0';
}
temp[len]='\0';
//res=Integer.valueOf(temp);
res=Integer.getInteger(temp);这两个函数都可以用的

4.Re:怎么把string中的数字读出来? [Re: Butterfly_125] Copy to clipboard
Posted by: andaywgz
Posted on: 2005-08-05 10:27

你可以用字符串类的类函数啊

5.Re:怎么把string中的数字读出来? [Re: Butterfly_125] Copy to clipboard
Posted by: xunloveru
Posted on: 2005-08-09 09:58

The best method is why's

import java.util.regex.*;
public class Test{
  public static void main(String[] args){
    String s = "ad8fas9d12342d2s";
    Pattern p=Pattern.compile("\\d+");
    Matcher m=p.matcherMoon;
    String sb=new String();
    while(m.find()) {sb=sb+m.group();}
  System.out.print(sb);
}
}

6.Re:怎么把string中的数字读出来? [Re: Butterfly_125] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-08-09 21:07

To xunloveru
Your code is good, but my suggestion is using StringBuffer instead of String in this case.


import java.util.regex.*;
public class Test{
public static void main(String[] args){
String s = "ad8fas9d12342d2s";
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcherMoon;
StringBuffer sb=new StringBuffer();
while(m.find()) {
sb.append(m.group());
}
System.out.print(sb);
}
}

7.Re:怎么把string中的数字读出来? [Re: gongshi] Copy to clipboard
Posted by: ww1ww1
Posted on: 2005-08-09 22:39

gongshi wrote:
To xunloveru
Your code is good, but my suggestion is using StringBuffer instead of String in this case.


Maybe StringBuilder is better.

8.Re:怎么把string中的数字读出来? [Re: ww1ww1] Copy to clipboard
Posted by: why
Posted on: 2005-08-10 01:00

ww1ww1 wrote:
Maybe StringBuilder is better.

StringBuilder is good, but it is only available in Java 5.0

9.Re:怎么把string中的数字读出来? [Re: why] Copy to clipboard
Posted by: ww1ww1
Posted on: 2005-08-11 01:09

why wrote:
StringBuilder is good, but it is only available in Java 5.0


Thumbs up


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923