2011-10-09 34 views
0

我有一個大腦放屁..我做錯了什麼...我的陣列關閉了?C#字符串數組字過濾器,我的數組是在索引之外?

public static string CleanBadwordsFromString(string text) { 

      string badWords = "bunch,of,words,that,do,not,need,to,be,seen"; 
      string[] badChars = badWords.Split(','); 
      string[] words = text.Split(' '); 
      int iLength = 0; 
      string sAttachtoEnd = null; 
      string cleanedString = ""; 
      int x = 0; 
      int i = 0; 

      //loop through our array of bad words 
      for (i = 0; i <= badChars.Length; i++) 
      { 
       //get the length of the bad word 
       iLength = badChars[i].Length; 
       //we are going to keep the first letter of the bad word and replace all the other 
       //letters with *, so we need to find out how many * to use 
       for (x = 1; x <= iLength - 1; x++) 
       { 
        sAttachtoEnd = sAttachtoEnd + "*"; 
       } 
       //replace any occurences of the bad word with the first letter of it and the 
       //rest of the letters replace with * 

       foreach (string s in words) 
       { 
        cleanedString =cleanedString + s.Replace(s, s.Substring(s.Length-1) + sAttachtoEnd); //should be: shit = s*** 
       } 
       sAttachtoEnd = ""; 
      } 
      return cleanedString; 


    } 

回答

1

我試着用i < badChar.Length解決方案運行你的代碼,儘管運行沒有錯誤,但結果並不符合我的預期。

我試圖運行這個命令:

CleanBadwordsFromString("Seen or not seen: Bunch, bunching, or bunched?") 

而且我得到了:

n****r****t****:****,****,****r****?****n*r*t*:*,*,*r*?*n****r****t****:****,****,****r****?****n***r***t***:***,***,***r***?***n*r*t*:*,*,*r*?*n**r**t**:**,**,**r**?**n***r***t***:***,***,***r***?***n*r*t*:*,*,*r*?*n*r*t*:*,*,*r*?*n***r***t***:***,***,***r***?***

顯然,這是不正確的。

我知道你的問題是關於數組索引,但我想你會想讓代碼正常工作。所以我想我可能會重寫,以使其工作。以下是我想出了:

public static string CleanBadwordsFromString(string text) 
{ 
    var badWords = 
     "bunch,of,words,that,do,not,need,to,be,seen" 
      .Split(',').Select(w => w.ToLowerInvariant()).ToArray(); 

    var query = 
     from i in Enumerable.Range(0, text.Length) 
     let rl = text.Length - i 
     from bw in badWords 
     let part = text 
      .Substring(i, Math.Min(rl, bw.Length)) 
     where bw == part.ToLowerInvariant() 
     select new 
     { 
      Index = i, 
      Replacement = part 
       .Substring(0, 1) 
       .PadRight(part.Length, '*') 
       .ToCharArray(), 
     }; 

    var textChars = text.ToCharArray(); 

    foreach (var x in query) 
    { 
     Array.Copy(
      x.Replacement, 0, 
      textChars, x.Index, x.Replacement.Length); 
    } 

    return new String(textChars); 
} 

現在我的結果是:

S*** or n** s***: B****, b****ing, or b****ed?

這看起來相當不錯。

我的方法並不依賴於在空間分割,所以會選擇標點符號和後綴。它也適用於源文本包含大寫字母的情況。

+0

我終於得到了我原來的工作......索引數組只是我的第一個bug ...很快就要去睡覺了。必須替換://用壞字的第一個字母替換壞字的所有出現,並用 替換掉其餘字母* text = text.Replace(badChars [i] .ToString(),badChars [i ] .Remove(1,badChars [i] .Length-1)+ sAttachtoEnd);我更喜歡你的解決方案,因爲我從來沒有想過標點符號或案例......感謝你的教訓 – Bryant

0
for (i = 0; i <= badChars.Length; i++) // Only < and not <= 

條件就是i < badChars.Length;。如果陣列長度是n那麼它的存取是從到n-1

如果數組長度爲,則在循環中嘗試訪問它不存在的第5個索引。

iLength = badChars[i].Length; // 5 <= 5 => true. But valid index is from 0 to 4 

這導致您的數組超出界限例外。