C#删除特定单词后的所有内容

时间:2017-04-17 12:46:11

标签: c#

我的意见是:

something1@email.com.;;
something2@email.eu,./
something3@email.org..
something4@email.netcdsfsd

我想要的是摆脱我的"域"(存储在数组中)之后的所有字符。

所以输出应该是:

something1@email.com
something2@email.eu
something3@email.org
something4@email.net

我的代码是:

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

它起作用但并不总是,最后只有一个角色。

我很乐意帮助

2 个答案:

答案 0 :(得分:1)

使用正则表达式检查电子邮件ID,而不是将其存储在不同的数组中

      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

希望这会有所帮助

答案 1 :(得分:0)

这很好用

string[] s = { ".com",".net",".edu",".eu" };
string[] domain = new string[]
{
"something1@email.com.;;",
"something2@email.eu,./",
"something3@email.org..",
"something4@email.netcdsfsd"
};
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);
                }
            }
        }
    }