-1

I want to replace a single comma (,) with (',').

For example:

"text,text,text" with "text','text','text".

I tried

MyText.Replace(',',"','");

but can't get anything working properly.

Any help would be appreciated.

Jasen
  • 14,030
  • 3
  • 51
  • 68
Roger
  • 3
  • 2
  • 4
    Remember that Replace (as any string method) returns a new string with the result of the operation. You need to reassign the result of replace to your string – Steve Feb 02 '16 at 18:30
  • Should the output have quotes on the ends, or be formatted as `text','text','text` ? – Sonny Childs Feb 02 '16 at 18:33

1 Answers1

4

Try:

MyText = MyText.Replace(",","','");

There are two .Replace methods on strings. One for single characters and one for strings. When you ',' that defines a single character so it goes to the single character version of the method. If you use double quotes then it defines a string so that version of the method is selected.

Docs on the string replace method: https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx

Daniel Slater
  • 4,123
  • 4
  • 28
  • 39
  • Thanks Daniel. This works I was trying to replace a char with string hence, resulting an invalid expression. – Roger Feb 02 '16 at 18:47