我需要从字符串中提取手机号码,获取手机号码。我从字符串中提取数字,但没有得到我需要的。识别与字符串
这里是输入:一个字符串包含的地址和电话号码。
String input = "Street1,Punjab Market-Patiala 147001 M:92166-29903"
我需要提取手机号码,这是92166-29903
。
我用这个方法来检查字符串是否包含一个手机号码或不:
private Boolean isMobileAvailable(String string)
{
Boolean ismobile = false;
if (string.matches("(?i).*M.*"))
{
ismobile = true;
}
return ismobile;
}
用来在代码如下:
String mobilenumber="";
private void getMobileNumber(String sample)
{
int index = sample.indexOf("M");
String newString = "";
newString = sample.substring(index, sample.length());
mobileNumber = newString.replaceAll("[^0-9]", "");
edtMobile.setText(mobileNumber.substring(0, 10));
}
在这里,而不是手机号码,我获得147009211
。
我怎样才能正确地识别来自上面的字符串的手机号码?
也许在这个岗位所提供的正则表达式是你在找什么:http://stackoverflow.com/questions/8634139/phone-validation-regex –
你有至少调试应用程序?你知道什么是错的吗? (提示:'sample.indexOf()'发现** **第一字符串的情况下...有一个为寻找** **最后彼此功能) –
什么类型做你的结果有诸如92166-29903?你在这里提到。 – starkshang