开发者

C# string array word filter, my array is outside of index? [closed]

开发者 https://www.devze.com 2023-04-12 05:34 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I'm having a brain fart..what am I doing wrong ...my array is off?

 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 o开发者_JS百科f 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;


    }


I tried to run your code with the i < badChar.Length solution and, even though it ran without error, the result was not what I expected.

I tried to run this:

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

And I got:

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***?***

Obviously that's not right.

I know your question was about the array index, but I figured that you'd want to then get the code to work correctly beyond that. So I thought how I might rewrite to make it work. Here's what I came up with:

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

Now my result is:

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

And that looks pretty good to me.

My approach doesn't rely on splitting on space so will pick up punctuation and suffixes. It also works if the source text contains uppercase.


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

The condition is just i < badChars.Length;. If the array length is n then, it's access is from 0 to n-1.

If the array length is 5, in the loop you are trying to access it's 5th index which don't really exist.

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

This is resulting you array out of bounds exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号