java怎么提取数字

[复制链接]
查看11 | 回复1 | 2010-4-5 21:57:21 | 显示全部楼层 |阅读模式
public class Test { public static void main(String[] args) {//源字符串String src = "Reply from 119.75.213.51: bytes=32 time=139ms TTL=52";//第一个等号后面的字符串:32 time=139ms TTL=52String s1 = src.substring(src.indexOf("=")+1, src.length());//s1第一个空格前面的数字String s2 = s1.substring(0, s1.indexOf(" "));int num1 = Integer.valueOf(s2);//s1获取第二个数字String s3 = s1.substring(s1.indexOf("=")+1, s1.indexOf("ms"));int num2 = Integer.valueOf(s3);//第三个数字String s4 = s1.substring(s1.lastIndexOf("=")+1, s1.length());int num3 = Integer.valueOf(s4);System.out.println(num1);System.out.println(num2);System.out.println(num3); }}
回复

使用道具 举报

千问 | 2010-4-5 21:57:21 | 显示全部楼层
//使用该方法可以提取任何字符串中的数字。。。。 public static void main(String []args) {String str = "reply from 119.75.213.51: bytes=32 time=139ms TTL=52";
char []ch = str.toCharArray();
//将字符串中的所有非数字替换成空格for(int i=0;i *** @since jdk 1.4 * @version 1.0 * */public class Main { public static void main(String[] args) {String s = "阿里145山1的61歌17哦13我7呢819如果";Pattern p = Pattern.compile("[0-9]");Matcher m = p.matcher(s);
while (m.find()) { System.out.println(m.group());} }}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行