ASP.Net/Language Basics/String
String concatenation (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
void page_Load() { string str; str = "Spring" + "Sale"; lblOut1.Text = str; string str2; DateTime datSaleSpring = Convert.ToDateTime("3/3/2005"); str2 = "Spring" + " " + "Sale begins " + Convert.ToString(datSaleSpring); lblOut2.Text = str2; lblSaleNote3.Text = "Spring" + " " + "Sale begins " + Convert.ToString(datSaleSpring); string str4; str4 = "Sale starts on 3/1/2005"; str4 = str4 + " and ends on " + txtSaleEnd.Text; lblSaleNote4.Text = str4; string str5; str5 = "Sale starts on 3/1/2005 and ends on "; str5 += txtSaleEnd.Text; lblSaleNote5.Text = str5; }
</script> <html>
<head> </head> <body> <form runat="server">
Example 1
<asp:Label runat="server" ID="lblOut1"/>
Example 2
<asp:Label runat="server" ID="lblOut2"/>
Example 3
<asp:Label runat="server" ID="lblSaleNote3"/>
Example 4
Sale End Date = <asp:TextBox runat="server" ID="txtSaleEnd"/> <asp:Button runat="server" Text="Button"></asp:Button>
<asp:Label runat="server" ID="lblSaleNote4"/>
Example 5
<asp:Label runat="server" ID="lblSaleNote5"/>
</form> </body>
</html>
</source>
String format (C#)
<source lang="csharp">
<%@ page language="C#" %> <script runat="server">
void BT_Submit_Click(object sender, System.EventArgs e) {
this.LT_Name.Text = string.Format("Your name is: {0}", this.TB_Name.Text);
}
</script> <html> <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form runat="server">
Please enter your name:
<asp:textbox id="TB_Name" runat="server"/>
<asp:button id="BT_Submit" runat="server" text="OK" onclick="BT_Submit_Click" />
<asp:literal id="LT_Name" runat="server"/>
</form>
</body> </html>
</source>
Sub a string value (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs) Dim fullString, subString As String Dim startPos, length As Integer startPos = CInt(TextBox2.Text) length = CInt(TextBox3.Text) fullString = TextBox1.Text subString = fullString.SubString(startPos, length) Label1.Text = subString End Sub
</script> <html> <head> </head> <body>
<form runat="server">
<asp:TextBox id="TextBox1" runat="server" TextMode="MultiLine" Width="259px" Height="112px"></asp:TextBox>
start: <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
length: <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
<asp:Label id="Label1" runat="server">Label</asp:Label>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Show Substring"></asp:Button>
</form>
</body> </html>
</source>