C# 从字符数组中删除除数字以外的任何元素
我有一个多行字符串文本,每行中有一个电话号码,由一些文本或空间字符(非英语字符)连接所以我想逐行检查文本,每行提取电话号码并将其保存在富文本框中。我面临一个问题,那就是如何从字符数组中删除除数字以外的任何元素。。任何帮助C# 从字符数组中删除除数字以外的任何元素,c#,arrays,winforms,C#,Arrays,Winforms,我有一个多行字符串文本,每行中有一个电话号码,由一些文本或空间字符(非英语字符)连接所以我想逐行检查文本,每行提取电话号码并将其保存在富文本框中。我面临一个问题,那就是如何从字符数组中删除除数字以外的任何元素。。任何帮助 using(StringReader reader=new StringReader(richTextBox1.Text)) { string line = string.Empty; do
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);
}}
使用(StringReader=新的StringReader(richTextBox1.Text))
{
string line=string.Empty;
做
{
line=reader.ReadLine();
如果(行!=null)
{
//检查行中是否包含字母以外的内容
char[]buffer=line.Replace(“,string.Empty).tocharray();
for(int i=0;i
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);
}}
不要使用字符数组。遍历字符串。字符串和数组都不允许删除元素,因此请构建一个新字符串。从一个空字符串变量开始,将希望保留的每个字符添加到该变量中。这就是你要找的for@EdPlunkett你能给我一个电话吗example@MohammedShfq这段代码是谁写的?我想保留可能需要@MohammedShfq的号码,所以,你会的,它只会返回numbers@MohammedShfq现在试试。