user974015
user974015

Reputation: 113

textbox replace text

It give me an error, don't know why. I want to replace ' with ".

try
{
     txtCS.Text.Replace("'", """);
}
catch
{
}

Upvotes: 2

Views: 16612

Answers (1)

keyboardP
keyboardP

Reputation: 69362

The Replace method returns a string because strings themselves are immutable. This means that instead of changing the existing string (txtCS.Text), it creates a new string object and so you need to assign that new string object to the textbox.

Also, you're missing the escape character in your quotes. By adding a \, you can use the " character, otherwise the compiler thinks you're closing the string.

txtCS.Text = txtCS.Text.Replace("'", "\""); 

Upvotes: 10

Related Questions