C#字符串数组字过滤器,我的数组是否在索引之外?

时间:2011-10-09 03:29:24

标签: c# arrays string

我有一个大脑放屁......我做错了什么...我的阵列已关闭?

 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;


    }

2 个答案:

答案 0 :(得分: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?

这对我来说非常好。

我的方法不依赖于分裂空间,因此会选择标点符号和后缀。如果源文本包含大写,它也可以使用。

答案 1 :(得分:0)

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

条件只是i < badChars.Length;。如果数组长度为 n ,那么它的访问权限从 0 n-1

如果数组长度为 5 ,则在循环中,您尝试访问它的第5个索引,该索引实际上并不存在。

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

这会导致数组超出范围异常。