只从android中的字符串中获取数字

时间:2012-11-21 13:15:59

标签: java android

我在微调器的getSelectedItem()上有这个字符串我只想要手机号码但显示名称。

Spinner spinnerthree = (Spinner) findViewById(R.id.spinner3);
String num=(String) spinnerthree.getSelectedItem();

返回结果是:

  

return num = 01111111111(abc)

2 个答案:

答案 0 :(得分:2)

使用正则表达式,如@keaukraine建议,以下是如何实现:

Spinner spinnerthree = (Spinner) findViewById(R.id.spinner3);
String num =(String) spinnerthree.getSelectedItem();  

Pattern intsOnly = Pattern.compile("\\d+");
Matcher makeMatch = intsOnly.matcher(num);
makeMatch.find();
String result = makeMatch.group();
Log.i("Pattern", result);

答案 1 :(得分:1)

使用正则表达式。 可以在这里找到很多例子:http://gskinner.com/RegExr/

选择标签'社区',类别'地址和电话'。

相关问题