|
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.
1 <%@ Import Namespace="System.IO" %> 2 <%@ Import Namespace="System.Text.RegularExpressions" %> 3 <script language="C#" runat="server"> 4 void Page_Load(Object Src, EventArgs E) { 5 String strTest; 6 strTest = "A sentence with stuff to replace."; 7 no_replace.Text = strTest; 8 strTest = Regex.Replace(strTest, "to replace", "replaced"); 9 replace.Text = strTest; 10 } 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>
1 <%@ Import Namespace="System.IO" %> 2 <%@ Import Namespace="System.Text.RegularExpressions" %> 3 <script language="VB" runat="server"> 4 Sub Page_Load(Src As Object, E As EventArgs) 5 Dim strTest As String 6 strTest = "A sentence with stuff to replace." 7 no_replace.text = strTest 8 strTest = Regex.Replace(strTest, "to replace", "replaced") 9 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>
//coming soon
|
| C# |
VB |
JScript |
|