-1

String not replacing Single Quotes with required characters

string abc = "STA\'ASTEST";
if (abc.Contains("'"))

{

abc.Replace("'", "\\'");                
}
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
Krunal
  • 11
  • 5

1 Answers1

2

You are doing the replace but not assigning the result to any variable.

I assume you want to assign the result to abc

string abc = "STA\'ASTEST"; 

if (abc.Contains("'"))
{
      abc = abc.Replace("'", "\'");
}

It is also redundant to have the if (abc.Contains("'")) because the Replace function will only replace if the expression to replace actually exists. So you can just write:

abc = abc.Replace("'", "\'");
NicoRiff
  • 4,803
  • 3
  • 25
  • 54