Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace " with " in c#

Tags:

c#

asp.net

In my website I am preparing an array using datagrid items in c#, which works fine, however when I have some text in the grid which has " as value code automatically replaces it with "

i have tried to replace " with " as below, however its failing and replacing " with \"

Array.Value += "|" + dgrFinal.Rows[i].Cells[1].Text.Replace
("amp;", "").Replace(""","\"")

is there any way i can replace " with ".

Can anyone please help me in this.....come on guys you are expert.

like image 522
user1717270 Avatar asked Feb 07 '13 11:02

user1717270


2 Answers

You can use HttpUtility. Here is an example.

using System.Web;

...

string value1 = "<html>";
string value2 = HttpUtility.HtmlDecode(value1);
string value3 = HttpUtility.HtmlEncode(value2);
Debug.WriteLine(value1);
Debug.WriteLine(value2);
Debug.WriteLine(value3);

This is the ouput:

<html>
<html>
&lt;html&gt;

I took the example from here.

like image 67
Luis Teijon Avatar answered Oct 05 '22 22:10

Luis Teijon


Have you tried

.Replace("&quot;", @"""")

Edit: outside of a GridView (I'm using LinqPad)

string xxx = "sd&quot;fd";
Console.WriteLine(xxx.Replace("&quot;", @""""));

returns:

sd"fd

If it's still appearing as \" then the battle is with the gridView - I haven't used one for ages but I'd be tempted to try and get the data right before binding to the grid, rather than looping through the rows and cells afterwards.

Edit2: I was going to say it could be the the debugger escaping the quotes but I'm days behind on this one. Good spot @Rawling. At least it's now solved :)

like image 42
Neil Thompson Avatar answered Oct 05 '22 22:10

Neil Thompson