我有一个多行字符串文本,每行都有一个由一些文字或空格字符连接的电话号码(字符不是英文)所以我想逐行检查文本,每行提取电话号码和把它保存在一个富文本框我面临一个问题,那就是如何从字符数组中删除除数字以外的任何元素..任何帮助!!
using(StringReader reader=new StringReader(richTextBox1.Text))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
// Checking if the line contains things other than letters
char[] buffer = line.Replace(" ", string.Empty).ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
if (!char.IsNumber(buffer[i]))
{
// Delete any letters or spacial characters
}
}
Regex rx = new Regex("^[730-9]{9}$");
if (rx.IsMatch(line))
{
richTextBox2.Text+=line;
}
}
} while (line != null);
}}
答案 0 :(得分:1)
Char.IsDigit
应该有所帮助:
string lineWithoutNumbers = line.Where(x => Char.IsDigit(x));
在你的情况下,像这样:
using(StringReader reader=new StringReader(richTextBox1.Text))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
// Checking if the line contains things other than letters
line = new String(line.Where(x => Char.IsDigit(x)).ToArray()); //here, we remove all non-digits from the line variable
Regex rx = new Regex("^[730-9]{9}$");
if (rx.IsMatch(line))
{
richTextBox2.Text+=line;
}
}
} while (line != null);
}}