M
Mad Scientist Jr
In terms of performance, is stringbuilder.replace noticably better or
worse than string.format ? String.format(mystring, myvalue) seems to be
made for this, but you are forced to use {0}, {1} etc as your tags,
where as with string.replace, you can choose tags that are more
readable, like <firstname/>, <lastname/> etc. I was playing around with
code to see how these work (below) - does one perform or scale notably
better? Is one more correct to use? Thanks
################################################################################################################################
HTML "test.aspx"
################################################################################################################################
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="test.aspx.vb" Inherits="MySite.MyProject.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table border="1">
<tr>
<th>
</th>
<th>
Stringbuilder</th>
<th>
String.format</th></tr>
<tr>
<td>Template:</td>
<td><asp:textbox id="TextBoxInA" runat="server"
Columns="20"></asp:textbox></td>
<td><asp:textbox id="TextBoxInB"
runat="server"></asp:textbox></td>
</tr>
<tr>
<td>Tag #1:</td>
<td><asp:TextBox id="TextBoxTagA1" runat="server"
Columns="20"></asp:TextBox></td>
<td>
{0}</td>
</tr>
<tr>
<td>Tag #2:</td>
<td><asp:TextBox id="TextBoxTagA2"
runat="server"></asp:TextBox></td>
<td>
{1}</td>
</tr>
<tr>
<td>Data #1:</td>
<td colspan="2" align="center"><asp:TextBox id="TextBoxData1"
runat="server" Columns="20"></asp:TextBox></td>
</tr>
<tr>
<td>Data #2:</td>
<td colspan="2" align="center"><asp:TextBox id="TextBoxData2"
runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td colspan="2" align="center"><asp:Button id="Button1"
runat="server" Text="Insert values"></asp:Button></td>
</tr>
<tr>
<td>Results:</td>
<td><asp:TextBox id="txtOutA" runat="server"
Rows="5"></asp:TextBox></td>
<td>
<asp:TextBox id="txtOutB" runat="server"></asp:TextBox></td>
</tr>
</table>
</form>
</body>
</HTML>
################################################################################################################################
CODEBEHIND "test.aspx.vb"
################################################################################################################################
Public Class test
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents TextBoxData1 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxData2 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxInA As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxTagA1 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxTagA2 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxInB As
System.Web.UI.WebControls.TextBox
Protected WithEvents txtOutA As System.Web.UI.WebControls.TextBox
Protected WithEvents txtOutB As System.Web.UI.WebControls.TextBox
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Me.IsPostBack() Then
Else
'Put user code to initialize the page here
TextBoxInA.Text = "My name is [first/] [last/]!"
TextBoxInB.Text = "My name is {0} {1}!"
TextBoxTagA1.Text = "[first/]"
TextBoxTagA2.Text = "[last/]"
TextBoxData1.Text = "Joe"
TextBoxData2.Text = "Bob"
txtOutA.Text = ""
txtOutB.Text = ""
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' STRINGBUILDER METHOD
Dim myStringBuilder1 As System.Text.StringBuilder
myStringBuilder1 = New System.Text.StringBuilder
myStringBuilder1.Append(TextBoxInA.Text)
myStringBuilder1.Replace(TextBoxTagA1.Text, TextBoxData1.Text)
myStringBuilder1.Replace(TextBoxTagA2.Text, TextBoxData2.Text)
txtOutA.Text = myStringBuilder1.ToString
' STRING.FORMAT METHOD
Dim myString1 As String
myString1 = TextBoxInB.Text
txtOutB.Text = myString1.Format(myString1, New Object()
{TextBoxData1.Text, TextBoxData2.Text})
End Sub
End Class
worse than string.format ? String.format(mystring, myvalue) seems to be
made for this, but you are forced to use {0}, {1} etc as your tags,
where as with string.replace, you can choose tags that are more
readable, like <firstname/>, <lastname/> etc. I was playing around with
code to see how these work (below) - does one perform or scale notably
better? Is one more correct to use? Thanks
################################################################################################################################
HTML "test.aspx"
################################################################################################################################
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="test.aspx.vb" Inherits="MySite.MyProject.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table border="1">
<tr>
<th>
</th>
<th>
Stringbuilder</th>
<th>
String.format</th></tr>
<tr>
<td>Template:</td>
<td><asp:textbox id="TextBoxInA" runat="server"
Columns="20"></asp:textbox></td>
<td><asp:textbox id="TextBoxInB"
runat="server"></asp:textbox></td>
</tr>
<tr>
<td>Tag #1:</td>
<td><asp:TextBox id="TextBoxTagA1" runat="server"
Columns="20"></asp:TextBox></td>
<td>
{0}</td>
</tr>
<tr>
<td>Tag #2:</td>
<td><asp:TextBox id="TextBoxTagA2"
runat="server"></asp:TextBox></td>
<td>
{1}</td>
</tr>
<tr>
<td>Data #1:</td>
<td colspan="2" align="center"><asp:TextBox id="TextBoxData1"
runat="server" Columns="20"></asp:TextBox></td>
</tr>
<tr>
<td>Data #2:</td>
<td colspan="2" align="center"><asp:TextBox id="TextBoxData2"
runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td colspan="2" align="center"><asp:Button id="Button1"
runat="server" Text="Insert values"></asp:Button></td>
</tr>
<tr>
<td>Results:</td>
<td><asp:TextBox id="txtOutA" runat="server"
Rows="5"></asp:TextBox></td>
<td>
<asp:TextBox id="txtOutB" runat="server"></asp:TextBox></td>
</tr>
</table>
</form>
</body>
</HTML>
################################################################################################################################
CODEBEHIND "test.aspx.vb"
################################################################################################################################
Public Class test
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents TextBoxData1 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxData2 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxInA As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxTagA1 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxTagA2 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBoxInB As
System.Web.UI.WebControls.TextBox
Protected WithEvents txtOutA As System.Web.UI.WebControls.TextBox
Protected WithEvents txtOutB As System.Web.UI.WebControls.TextBox
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Me.IsPostBack() Then
Else
'Put user code to initialize the page here
TextBoxInA.Text = "My name is [first/] [last/]!"
TextBoxInB.Text = "My name is {0} {1}!"
TextBoxTagA1.Text = "[first/]"
TextBoxTagA2.Text = "[last/]"
TextBoxData1.Text = "Joe"
TextBoxData2.Text = "Bob"
txtOutA.Text = ""
txtOutB.Text = ""
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' STRINGBUILDER METHOD
Dim myStringBuilder1 As System.Text.StringBuilder
myStringBuilder1 = New System.Text.StringBuilder
myStringBuilder1.Append(TextBoxInA.Text)
myStringBuilder1.Replace(TextBoxTagA1.Text, TextBoxData1.Text)
myStringBuilder1.Replace(TextBoxTagA2.Text, TextBoxData2.Text)
txtOutA.Text = myStringBuilder1.ToString
' STRING.FORMAT METHOD
Dim myString1 As String
myString1 = TextBoxInB.Text
txtOutB.Text = myString1.Format(myString1, New Object()
{TextBoxData1.Text, TextBoxData2.Text})
End Sub
End Class