-1

I am attempting to replace characters in a string data type. Below is my code, but the ' is not being replaced with ''. Am I mis-understanding how the replace() function operates? What should I do in order to change the string to High Flying Picture''s?

This is the syntax I attempted.

public static void Test()
{
    string strVar = "High Flying Picture's";
    strVar.Replace("'", "''");
    Console.WriteLine(strVar);
}
Yohan Greenburg
  • 333
  • 1
  • 3
  • 9
  • **DON'T DO THIS!** Replacing the `'` character with two of them almost always has to do with a fix for strings that will be using in SQL statements, and this technique is **WRONG** and _WILL_ result in your code getting hacked. Instead, research parameterized queries. – Joel Coehoorn Feb 12 '17 at 22:26
  • @JoelCoehoorn - I am using C# and the Microsoft Excel Interop to create hyperlinks from a Master Worksheet to Sub Worksheets. Sometimes the Name in the Master Sheet (like High Flying Picture's) has an apostrophe in the name, and in order to properly create the hyperlink I need to double the apostrophe. Since I am new to the world of C# - I will keep this in mind for when I am writing parameter queries. – Yohan Greenburg Feb 12 '17 at 22:50
  • I do not feel that the duplicate ? posed really is a duplicate of my question. That question gives a great explanation of how to replace paths, but I was needing to replace a character in a string. However, after seeing the answer from @JohanP - I see that the baseline is the same of me just needing to alter syntax to `strVar = strVar.Replace("'", "''");` so the basis is the same. But for a new learner like myself, it would be difficult to follow IMO – Yohan Greenburg Feb 12 '17 at 22:53

1 Answers1

1
strVar = strVar.Replace("'", "''");

You need to reassign it.

JohanP
  • 5,252
  • 2
  • 24
  • 34