C# .NET - To replace the contents in a string - Asked By abinav shankar on 27-Jan-12 01:04 AM

Hi
 
I have a text file in which i have some HTML tags and I am reading this text file using a stream reader, now the contents of the string looks like the code displayed below
now in that string format i have to replace #Name# with the Myname

<html>
<body>
<h4>This is an Auto generated mail Please do not reply to this message</h4>
<table border=1>
<tr>
<td>Name</td>
<td>Department</td>
<td>Designation</td>
</tr>
<tr>  
<td>#Name#</td>
<td>#Department#</td>
<td>#Designation#</td
</tr>
</table>
</body>
</html>
I want to replace the things with #Name#,#designation# etc with original data from code behind pl help me in doing it
Riley K replied to abinav shankar on 27-Jan-12 01:17 AM

Try to place a AspLiteral control in TD

check the below code

//aspx page
 <table>
       <tr>
        <td>
        <asp:Literal ID="lit" runat="server"></asp:Literal>
        </td>
       </tr>
    </table>

//code behind
 protected void Page_Load(object sender, EventArgs e)
    {
        lit.Text = "Your text here";
    }

Regards.
Danasegarane Arunachalam replied to abinav shankar on 27-Jan-12 02:16 AM
Take the help of regular Expression for this one

The below code replaces the #Name# with myname and implement for other things also


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim str As String = "tr>  <d>#Name#</td><td>#Department#</td><td>#Designation#</td> </tr> "
    Dim replacedStr As String = System.Text.RegularExpressions.Regex.Replace(str, "#Name#", "MyName")
 
  End Sub
Jitendra Faye replied to abinav shankar on 27-Jan-12 04:05 AM
first, store all string in one string variable.

then use Replace () of string class.

like this  -

string str= strAll.Replace("#Name#","#Designation#")