2013-06-24 219 views
1

我有一個不好的單詞列表。如果一個字符串包含壞字列表中的任何項目/項目,我需要從字符串中刪除該壞字。C# - 查找並從字符串列表中刪除子串

List<string> badWordList = new List<string> { "email:", "index", "mobile:", "fax:", "web" }; 

我能夠搜索字符串,但無法刪除。請幫我...我嘗試下面的代碼:

string myText = "email:[email protected]"; 
if (badWordList.Any(w => myText.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0)) 
{ 
    // How to remove 
} 

下面是一組期望輸出的輸入:

  1. I/P-電子郵件:[email protected]

    O/p - [email protected]

  2. I /對 - 傑克F.手機:89788987

    O /對 - 傑克F. 89788987

  3. I/P-傑克F.電子郵件:T @家個人電腦移動:65777 WEB

    O/P-傑克F. T @家個人電腦65777

我寧願一個非正則表達式做法。謝謝你的幫助。

+0

用正則表達式的任何問題?這總是更快,更方便。 – Kangkan

+0

'myText = myText.Replace(badWordList.First(i => myText.IndexOf(w,StringComparison.OrdinalIgnoreCase)> = 0),「」);'通常,如果這要處理用戶輸入,請確保它不會。 – Leri

+0

@Kangkan:Regex沒問題。 – SKJ

回答

9

可以遍歷的髒話並刪除它們:

foreach (string badWord in badWordList) { 
    myText = myText.Replace(badWord, string.Empty); 
} 

注意,但是,這種方法是區分大小寫的,即它會刪除「郵件:」但不是「EMAIL」。
如果你需要一個不區分大小寫的解決方案,最簡單的方法是使用正則表達式:

string myText = "EMAIL:[email protected]"; 
Regex badWords = new Regex("email:|index|mobile:|fax:|web", RegexOptions.IgnoreCase | RegexOptions.Compiled); 
myText = badWords.Replace(myText, string.Empty); 
4

您可以用空字符串替換它們刪除字符串:

foreach (var badWord in badWordList) 
{ 
    myText = myText.Replace(badWord, ""); 
} 

Unfortulately這情況敏感。對於不區分大小寫的字符串替換沒有正則表達式,見Is there a case insensitive string replace in .Net without using Regex?

您也可以用正則表達式做,在這種情況下不區分大小寫的比較來「免費」:

var regex = String.Join("|", badWordList.Select(w => Regex.Escape(w))); 
var myText = Regex.replace(myText, regex, "", RegexOptions.IgnoreCase); 
+0

在開始時,我想我會去非正則表達式的方法。但現在,我認爲正則表達式會很有幫助。謝謝! – SKJ

1

替換實例'壞詞' 與string.Empty: -

List<string> badWordList = new List<string> { "email", "index:", "mobile:", "fax:", "web" }; 
string myText = "email:[email protected]"; 

foreach (string s in badWordList) 
{ 
    myText = myText.Replace(s,string.Empty); 
} 
+3

我只想說'IndexOf'檢查是不必要的,因爲'.Replace'不會取代任何其他字符串中不存在的字符串。 – Arran

0

如果它是區分大小寫:

List<String> badWordList = new List<String> { "email:", "index", "mobile:", "fax:", "web" }; 
String myText = "This is a exemple of Email: deleting with index and fax and web"; 

badWordList.ForEach(bw => myText = myText.Replace(bw, String.Empty)); 
1
  using System; 
      using System.Collections.Generic; 
      using System.Linq; 
      using System.Text; 
      using System.Collections; 
      namespace WindowMaker 
      { 
       class Program 
       { 

        static void Main(string[] args) 
        { 
         System.Console.WriteLine("Enter Main string..."); 
         String str = System.Console.ReadLine(); 
         System.Console.WriteLine("Enter sub string..."); 
         String sub = System.Console.ReadLine(); 
         Boolean flag; 
         int strlen=sub.Length; 
         int inde = str.IndexOf(sub); 
         while (inde != -1) 
         { 
          inde = str.IndexOf(sub); 
          str=str.Replace(sub,""); 

         } 
         System.Console.WriteLine("Remaining string :: {0}",str); 

          Console.Read(); 
        } 

       } 
      } 

enter image description here