Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:50:36+00:00 2026-05-26T00:50:36+00:00

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

  • 0

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 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:50:36+00:00Added an answer on May 26, 2026 at 12:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.