寻找一个电话号码字符串 [英] Look for a phone number in a string
本文介绍了寻找一个电话号码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题的字符串找到电话号码。
我有功能:
公共无效getPhoneNumber()
{
模式intsOnly = Pattern.compile(\\\\ D +);
匹配makeMatch = intsOnly.matcher(数);
makeMatch.find();
字符串结果= makeMatch.group();
Log.i(模式,结果);
}
不过,我有不好的结果。
我的字符串:
串号=字符串字符串s。秒。海峡23-232 12 23 adsdsa
解决方案
我写这样的:
这解决了我的问题:)
公共字符串getPhoneNumber()
{
的char []临时= numer.toCharArray();
字符串值=;
INT licz = 0; 的for(int i = 0; I< temp.length;我++)
{
如果(licz&10 9)
{
如果(Character.toString(TEMP [I])。比赛([0-9]))
{
值+ = Character.toString(TEMP [I]);
licznik ++;
}
否则如果(Character.toString(温度[I])匹配(\\ u0020 | \\\\ - | \\\\(| \\\\)。))
{ }
其他
{
值=;
licz = 0;
}
}
} 如果(value.length()!= 9)
{
值= NULL;
}
其他
{
值=电话:+ value.trim();
} 返回值;
}
I have problem with finding phone number in string.
I have function:
public void getPhoneNumber()
{
Pattern intsOnly = Pattern.compile("\\d+");
Matcher makeMatch = intsOnly.matcher(number);
makeMatch.find();
String result = makeMatch.group();
Log.i("Pattern", result);
}
But I have bad result.
My string:
String number = "String string s. s. str. 23-232 12 23 adsdsa"
解决方案
I wrote this:
This solved my problem :)
public String getPhoneNumber()
{
char[] temp = numer.toCharArray();
String value="";
int licz=0;
for(int i=0;i<temp.length;i++)
{
if(licz<9)
{
if(Character.toString(temp[i]).matches("[0-9]"))
{
value+=Character.toString(temp[i]);
licznik++;
}
else if(Character.toString(temp[i]).matches("\u0020|\\-|\\(|\\)"))
{
}
else
{
value="";
licz=0;
}
}
}
if(value.length()!=9)
{
value=null;
}
else
{
value="tel:"+value.trim();
}
return value;
}
这篇关于寻找一个电话号码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文