从字符串中提取数字只 [英] Extracting only digits from a String

查看:118
本文介绍了从字符串中提取数字只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java字符串,我需要从中提取仅第一个数字。

I have a Java String, i need to extract only the first digit from it.

例如字符串:

2鱼3

我只想得到:2,这是我的code:

I want to get only: "2" and this is my code:

String num1 = mEtfirst.getText().toString(); 
num1 = num1.replaceAll("[^0-9.,]+",""); 

以上code接受两个数字这是2和32鱼类3我想实现的是只接受或提取2,并将其放置NUM1。可能吗?

the code above accepts the two numbers which are 2 and 3 in "2 fishes 3" what i wish to achieve is to only accept or extract "2" and place it to num1. is it possible?

推荐答案

像萨德克指出,使用常规EX pressions

String myString ="2 fishes 3"
myString = myString .replaceAll("\\D+","");

的结果的myString 仅仅是数字。

在这里查看运行的例子:

check the running example here:

http://ideone.com/Xg4xFc

这篇关于从字符串中提取数字只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆