Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing double quote with a single quote

Tags:

c#

replace

I have the following string in c#:

string ptFirstName = tboxFirstName.Text;

ptFirstName returns: "John"

I wish to convert this to 'John'

I have tried numerous variations of the following, but I am never able to replace double quotes with single quotes:

ptFirstName.Replace("\"", "'");

Can anybody enlighten me?

My goal is to write this to an XML file:

writer.WriteAttributeString("first",ptFirstName);   // where ptFirstName is 'John' in single quotes.
like image 560
user2058253 Avatar asked Mar 17 '13 01:03

user2058253


1 Answers

The reason

ptFirstName.Replace("\"", "'");

does not work is that string is immutable. You need to use

ptFirstName = ptFirstName.Replace("\"", "'");

instead. Here is a demo on ideone.

like image 63
Sergey Kalinichenko Avatar answered Sep 22 '22 14:09

Sergey Kalinichenko