从字符串中识别并获取手机号码
Identify and get a mobile number from a String
我需要从字符串中提取手机号码。我已经从字符串中提取了数字,但没有得到我需要的。
这是输入:一个包含地址和 phone 数字的字符串。
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
。
如何从上面的字符串中正确识别手机号码?
int index = sample.lastIndexOf("M");
String newString = sample.substring(index+2, sample.length());
mobileNumber = newString.replaceAll("[^0-9]", "");
edtMobile.setText(mobileNumber.substring(0, 10));
如果您想要结果中的“-”,只需删除
mobileNumber = newString.replaceAll("[^0-9]", "");
试试这个代码。
方法一:
String input = "Street1,Punjab Market-Patiala 147001 M:92166-29903";
int value = input.lastIndexOf(":");
String s = input.substring(value + 1, input.length());
Log.d("reverseString", "" + s);
方法二:
StringBuffer reverseString=new StringBuffer(input);
StringBuffer s1=reverseString.reverse();
int firstindex=s1.indexOf(":");
String finalstring=s1.substring(0, firstindex);
StringBuffer getexactstring=new StringBuffer(finalstring);
此代码将为您提供所需的确切手机号码。
我需要从字符串中提取手机号码。我已经从字符串中提取了数字,但没有得到我需要的。
这是输入:一个包含地址和 phone 数字的字符串。
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
。
如何从上面的字符串中正确识别手机号码?
int index = sample.lastIndexOf("M");
String newString = sample.substring(index+2, sample.length());
mobileNumber = newString.replaceAll("[^0-9]", "");
edtMobile.setText(mobileNumber.substring(0, 10));
如果您想要结果中的“-”,只需删除
mobileNumber = newString.replaceAll("[^0-9]", "");
试试这个代码。 方法一:
String input = "Street1,Punjab Market-Patiala 147001 M:92166-29903";
int value = input.lastIndexOf(":");
String s = input.substring(value + 1, input.length());
Log.d("reverseString", "" + s);
方法二:
StringBuffer reverseString=new StringBuffer(input);
StringBuffer s1=reverseString.reverse();
int firstindex=s1.indexOf(":");
String finalstring=s1.substring(0, firstindex);
StringBuffer getexactstring=new StringBuffer(finalstring);
此代码将为您提供所需的确切手机号码。