2011-10-09 62 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 

这导致您的数组超出界限例外。