-2

I want to make an application that filter all bad words from a database in a string. The problem is that the replace isn't working.

private void checkmessage(string message)
{
    try
    {

        if (string.IsNullOrEmpty(message))
        {
            MessageBox.Show("foutmelding 102, neem contact op met de beheerder");
        } else
        {
            ArrayList names = new ArrayList();
            SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=twitterwall;Integrated Security=True");
            SqlCommand command = new SqlCommand
            (
                "SELECT * FROM blacklist", conn
            );
            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    names.Add(reader["name"].ToString());
                }
                conn.Close();
            } catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            foreach (string x in names)
            {
                if (message.Contains(x))
                {
                    message.Replace(x, " ***** ");
                }
            }
            txtbericht.Text = "completed " + message;
        }

    } catch {
        MessageBox.Show("Foutmelding 101");
    }

}
LordWilmore
  • 2,829
  • 2
  • 25
  • 30
dennis
  • 39
  • 6

2 Answers2

3

"Not working" is not a question but I'll try to point you in the right direction into "working" version.

I would suggest you to read documentation about Replace method.

And you will find this fragment in documentation:

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

But in your code there's "

if (message.Contains(x))
{
    message.Replace(x, " ***** ");
}

To fix this issue you have to use the return value from Replace method :

if (message.Contains(x))
{
    message = message.Replace(x, " ***** ");
}
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
0

String Replace() doesn't do in-situ replacement, it returns a new string with the values replaced as requested.

LordWilmore
  • 2,829
  • 2
  • 25
  • 30
  • 1
    That´s the explanation. Now how add how to solve it. – MakePeaceGreatAgain Dec 21 '16 at 10:59
  • 2
    This isn't a very good answer. It's probably right, but showing how and where this applies to the OP's question would improve the quality significantly. – Enigmativity Dec 21 '16 at 10:59
  • The post shows exactly the problem which is simply down to a misunderstanding of a library function. I imagine the poster knows how to use a return value. – LordWilmore Dec 21 '16 at 11:01
  • 2
    Bah. Its the perfect answer. Short, and to the point. Any more typing would just be chatter and insulting to the OP. – Sam Axe Dec 21 '16 at 11:01
  • 1
    The answer will probably be viewed by alot more people than just the OP, but I do agree answers shouldn't need copy-paste ready solutions – C.Evenhuis Dec 21 '16 at 11:04
  • 1
    Again, if anybody else reading the answer doesn't know what to do with a return value then they need more help than I am willing to provide here. – LordWilmore Dec 21 '16 at 11:12
  • While only pointing out the problem and leaving the solution up to the reader may make you feel good about yourself, it isn't particularly helpful nor the goal of this site. Answers should be self-contained and contain an actual answer. Just pointing out what is wrong is only half an answer. – CodeCaster Dec 21 '16 at 12:13