-2

I have a multi line string text that has in each line a phone number joined by some text or spacial characters(characters not in English) so I want to check the text line by line and for each line extract the phone number and keep it in a rich text box I faced one problem and that is how to Remove any element other than numbers from character array .. any help !!

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);

            }}
Mohammed Shfq
  • 65
  • 2
  • 11

1 Answers1

1

Char.IsDigit should help:

string lineWithoutNumbers = line.Where(x => Char.IsDigit(x));

In your case, something like this:

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);

        }}
Yurii N.
  • 5,455
  • 12
  • 42
  • 66