2017-04-17 32 views
0

我輸入的是:C#特定的單詞後刪除一切

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

我要的是經過我的「域」(存儲在陣列),以擺脫所有的字符。

所以輸出應該是:

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

我的代碼是:

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

它的工作,但並不總是在最後只有一個字符。

我會很樂意幫忙

+0

你可以用一個簡單的正則表達式來解決它,例如:'(。*?@。*?\ .net | eu | org)' – Cybrus

回答

0

這工作得很好

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); 
       } 
      } 
     } 
    } 
+0

我將使用該解決方案,謝謝。 – user7878641

+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 

希望這有助於

+0

老實說,我不會真的k現在該怎麼處理正則表達式。我如何將它導出到其他richtextbox或數組? – user7878641

+0

對不起,我認爲你想要所有的匹配值現在上面的代碼將爲你工作。這也將匹配與兩個「時期」的域名 – Sameer

+1

謝謝。我現在明白了。 – user7878641

相關問題