'C# How to delete certain part of string from text not to delete the rest text [duplicate]

Like in the title, how to delete a certain part of text without deleting the whole text. The example how it should work.

"Some text I put in the richtextbox. Date time 14.05.2022" -> after pressing button/checkbox the result->: "Some text I put in the richtextbox."

Some code:

    private void checkBox30_CheckStateChanged(object sender, EventArgs e)
    {
        if (checkBox30.CheckState == CheckState.Checked)
        {
            richtextbox.Text += "Some text I put in the richtextbox. Date time 14.05.2022"; 
        }
        else
        {
           richtextbox.Text.Replace("Date time 14.05.2022","");
        }
    }

The replace method doesn't work. Nothing happens after clicking a checkbox.



Solution 1:[1]

This should work. You missed assigning the output back to the original variable.

private void checkBox30_CheckStateChanged(object sender, EventArgs e)
{
    if (checkBox30.CheckState == CheckState.Checked)
    {
        richtextbox.Text += "Some text I put in the richtextbox. Date time 14.05.2022"; 
    }
    else
    {
        richtextbox.Text = richtextbox.Text.Replace("Date time 14.05.2022","");
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benison Sam