user1717270
user1717270

Reputation: 785

Replace " with " in c#

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.

Upvotes: 6

Views: 25808

Answers (3)

Luis Teijon
Luis Teijon

Reputation: 4899

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.

Upvotes: 13

Yannick Katambo
Yannick Katambo

Reputation: 41

Asp.net GridViewRow:

row.Cells[3].Text.Trim().Replace("&quot;", @"""")

Before: &quot;Gasket 22&quot;&quot; rubber ventlation pipes&quot;

After: "Gasket 22"" rubber ventlation pipes"

Upvotes: 0

Neil Thompson
Neil Thompson

Reputation: 6425

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 :)

Upvotes: 7

Related Questions