user7878641
user7878641

Reputation: 13

C# Remove everything after specific word

My input is:

[email protected].;;
[email protected],./
[email protected]..
[email protected]

What I want is to get rid of all characters after my "domains"(stored in array).

So output should be:

[email protected]
[email protected]
[email protected]
[email protected]

My code is:

string[] domains = richTextBox_domains.Text.Split(';');
char[] specialchars = ".!#$%^&*()-_=+[]{}\\|;:'\",.<>/?".ToCharArray();
for (int x = 0; x < domains.Length; x++)
                        {
                            for (int y = 0; y < specialchars.Length; y++)
                            {
                                string check = domains[x] + specialchars[y];
                                string aftertmp = "." + after.Substring(after.IndexOf('.') + 1);
                                if (aftertmp == check)
                                    after = after.Replace(check, domains[x]);
                            }
                        }

It's working but not always and only for one character at the end.

I will be glad for help

Upvotes: 0

Views: 1336

Answers (2)

Sameer
Sameer

Reputation: 705

use regex to check email id and than store it in different array

      var regex1 = new Regex("(([-a-zA-Z0-9])|([-a-zA-Z0-9]{1,256}[@%._\+~#=][-a-zA-Z0-9])){1,10}\.[-a-zA-Z0-9]{1,10}\b",RegexOptions.IgnoreCase);
      string[] domains = richTextBox_domains.Text.Split(';');
      List<string> l = new List<string>();
      for (int x = 0; x < domains.Length; x++)
      {
         var results = regex1.Matches(domains[x]);
         foreach (Match match in results)
         {
            l.Add(match.Groups[1].Value);
            MessageBox.Show(match.Groups[1].Value);
         }
      }

      string[] s = l.ToArray();// new array

hope this helps

Upvotes: 1

Ahmed Elshorbagy
Ahmed Elshorbagy

Reputation: 362

This works fine

string[] s = { ".com",".net",".edu",".eu" };
string[] domain = new string[]
{
"[email protected].;;",
"[email protected],./",
"[email protected]..",
"[email protected]"
};
string result = "";
        for (int m = 0; m < domain.Length; m++)
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (domain[m].IndexOf(s[i]) != -1)
                {
                    result = domain[m].Substring(0, domain[m].IndexOf(s[i])) + s[i];
                    MessageBox.Show(result);
                }
            }
        }
    }

Upvotes: 0

Related Questions