Replace() In .NET

The ASPSmith Articles, ASPAlliance.com

Replace() In .NET

By Steven Smith
http://www.aspalliance.com/stevesmith/articles/dotnetreplace.asp
[Example/VB]
[Example/C#]

Sometimes it's the little things. Since I just spent about an hour trying to track down how to do a simple Replace() in .NET (you'd think it would be a String class method, wouldn't you?), I figure it's worth a (short) article on the topic. Hopefully this will keep a few of you from wasting time trying to find where Microsoft has hidden the more common VBScript functions, like Replace.

Just to make this article interesting, I'll list the code in all three standard languages of .NET: VB, C#, and JScript. I may even add a working example at some point.

   <%@ Import Namespace="System.IO" %>
   <%@ Import Namespace="System.Text.RegularExpressions" %>
   <script language="VB" runat="server">
   Sub Page_Load(Src As Object, E As EventArgs)
    Dim strTest As String
    strTest = "A sentence with stuff to replace."
    no_replace.text = strTest
    strTest = Regex.Replace(strTest, "to replace", "replaced")
    replace.text = strTest
10   End Sub
11   </script>
12   <html>
13   <body>
14   Original Text: <asp:label id="no_replace" runat="server"/>
15   <hr />
16   Replaced Text: <asp:label id="replace" runat="server"/>
17   </body>
18   </html>
C# VB JScript