我有一行多行字符串文本,每行都有一些電話號碼加上一些文本或空間字符(字符不是英文),所以我想逐行檢查文本併爲每一行提取電話號碼,並保存在一個豐富的文本框中,我面臨一個問題,那就是如何從字符數組中除去數字以外的任何元素..任何幫助!從字符數組中除去數字以外的任何元素
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);
}}
不要使用字符數組。遍歷字符串。字符串和數組都不允許刪除元素,所以建立一個* new *字符串。從一個空的字符串變量開始,併爲其添加您希望保留的每個字符。 –
[This](https://stackoverflow.com/a/3210462/2457029)是你在找什麼 – maccettura
@EdPlunkett你可以舉個例子 –