Progress Monitor for IdealProgrammer.com
Take Swift, Intelligent, Massive, Planned, Loving, Effective (S.I.M.P.L.E.) Action to Transform Self into Ideal
Skip Repetitive Navigational Links
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console
Please login
C# ASP.NET String Examples
Description:
Illustrates using String Properties and String Methods in C-Sharp ASP.NET.
Example Webform Code:
< %@ Page Language="C#" AutoEventWireup="true" CodeFile="String.aspx.cs" Inherits="LanguageBasics_String" %> < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp :Label ID="Label1a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label1" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label2a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label2" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label3a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label3" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label4a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label4" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label5a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label5" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label6a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label6" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label7a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label7" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label8a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label8" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label9a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label9" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label10a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label10" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label11a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label11" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label12a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label12" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label13a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label13" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label14a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label14" runat="server" Text="Label"></asp><br /><br /> </div> </form> </body> </html>
Example Code Behind:
partial class LanguageBasics_String : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { //**************************************************************************************** // Example #1: StartsWith(string) // Example #2: EndsWith(string) // Example #3: IndexOf(string[, startIndex][,count]) // Example #4: LastIndexOf(string[, startIndex][,count]) // Example #5: Insert(startIndex, string) // Example #6: PadLeft(totalWidth [, paddingCharacter]) // Example #7: PadRight(totalWidth [, paddingCharacter]) // Example #8: Remove(startIndex, count) // Example #9: Replace(oldString, newString) // Example #10: Substring(startIndex[, length]) // Example #11: ToLower // Example #12: ToUpper // Example #13: Trim // Example #14: Length //**************************************************************************************** //**************************************************************************************** // Example #1: StartsWith(string) // Returns a Boolean value that indicates if the string starts with the specified string. //**************************************************************************************** Label1a.Text = "Example #1: StartsWith(string) "; string strStartsWithExample = "This is a test string"; Label1.Text = strStartsWithExample.StartsWith("This").ToString(); //Returns True //**************************************************************************************** // Example #2: EndsWith(string) // Returns a Boolean value that indicates if the string ends with the specified string. //**************************************************************************************** Label2a.Text = "Example #2: EndsWith(string) "; string strEndsWithExample = "This is a test string"; Label2.Text = strEndsWithExample.EndsWith("string").ToString(); //Returns True //**************************************************************************************** // Example #3: IndexOf(string[, startIndex][,count]) // Returns an integer that represents the position of the first occurrence of the // specified number of characters. If you specify startIndex, that specifies // where to start looking. If you specify count, that specifies how many characters to // to search past start. If the string is not found, this method returns -1 //**************************************************************************************** Label3a.Text = "Example #3: IndexOf(string[, startIndex][,count]) "; string strIndexOfExample = "This is a test string"; Label3.Text = strIndexOfExample.IndexOf("is").ToString(); //Returns 2 //**************************************************************************************** // Example #4: LastIndexOf(string[, startIndex][,count]) // Returns an integer that represents the position of the last occurrence of the // specified number of characters. If you specify startIndex, that specifies // where to start looking. If you specify count, that specifies how many characters to // to search past start. If the string is not found, this method returns -1 //**************************************************************************************** Label4a.Text = "Example #4: LastIndexOf(string[, startIndex][,count]) "; string strLastIndexOfExample = "This is a test string"; Label4.Text = strLastIndexOfExample.LastIndexOf("is").ToString(); //Returns 5 //**************************************************************************************** // Example #5: Insert(startIndex, string) // Returns a string with the specified string inserted beginning at the specified position. //**************************************************************************************** Label5a.Text = "Example #5: Insert(startIndex, string) "; string strInsertExample = "This is a test string"; int intIndex = 0; // First find position of word test intIndex = strInsertExample.IndexOf("test"); // Insert "simple" before "test" Label5.Text = strInsertExample.Insert(intIndex, "simple "); //**************************************************************************************** // Example #6: PadLeft(totalWidth [, paddingCharacter]) // Returns a string that is right-aligned and padded on left with character specified. // If no padddingCharacter is specified, space is used. totalWidth is total width of column // This allows you to create a nice straight column even though lengths of text vary. //**************************************************************************************** Label6a.Text = "Example #6: PadLeft(totalWidth [, paddingCharacter]) "; string strPadLeftExampleA = "This is a test string"; // PadLeft "simple" before "test" Label6.Text = strPadLeftExampleA.PadLeft(35); //**************************************************************************************** // Example #7: PadRight(totalWidth [, paddingCharacter]) // Returns a string that is left-aligned and padded on right with character specified. // If no padddingCharacter is specified, space is used. totalWidth is total width of column // This allows you to create a nice straight column even though lengths of text vary. //**************************************************************************************** Label7a.Text = "Example #7: PadRight(totalWidth [, paddingCharacter]) "; string strPadRightExampleA = "This is a test string"; // PadRight "simple" before "test" Label7.Text = strPadRightExampleA.PadRight(35) + "next column starts here"; //**************************************************************************************** // Example #8: Remove(startIndex, count) // Returns a string with the specified number of characters removed starting at startIndex. // If no Count is specified, the remainder of string is removed //**************************************************************************************** Label8a.Text = "Example #8: Remove(startIndex, count) "; string strRemoveExample = "This is a test string"; int intIndexRemove = 0; // First find position of word test intIndexRemove = strRemoveExample.IndexOf("test"); // Remove rest of string starting with word "test" Label8.Text = strRemoveExample.Remove(intIndex); //**************************************************************************************** // Example #9: Replace(oldString, newString) // Returns a string with all occurences of oldString replaced by newString //**************************************************************************************** Label9a.Text = "Example #9: Replace(oldString, newString) "; string strReplaceExample = "This is a test string"; // Replace "This" with "That" Label9.Text = strReplaceExample.Replace("This", "That"); //**************************************************************************************** // Example #10: Substring(startIndex[, length]) // Returns the string that starts at the specified position and has the specified // length. If the length is not specified, all of the characters to the end are returned. //**************************************************************************************** Label10a.Text = "Example #10: Substring(startIndex[, length]) "; string strSubstringExample = "This is a test string"; int intIndexSubstring = 0; // First find position of word test intIndexSubstring = strRemoveExample.IndexOf("test"); // Get everything starting at "test" to end of string Label10.Text = strSubstringExample.Substring(intIndexSubstring); //**************************************************************************************** // Example #11: ToLower // Returns the string in Lowercase //**************************************************************************************** Label11a.Text = "Example #11: ToLower"; string strToLowerExample = "This is a test string"; // Return Lowercase Label11.Text = strToLowerExample.ToLower().ToString(); //**************************************************************************************** // Example #12: ToUpper // Returns the string in Uppercase //**************************************************************************************** Label12a.Text = "Example #12: ToUpper "; string strToUpperExample = "This is a test string"; // Return Uppercase Label12.Text = strToUpperExample.ToUpper().ToString(); //**************************************************************************************** // Example #13: Trim // Returns the string with leading and trailing spaces removed //**************************************************************************************** Label13a.Text = "Example #13: Trim "; string strTrimExample = " ABC "; // Return Uppercase Label13.Text = strTrimExample.Trim().Length.ToString(); //**************************************************************************************** // Example #14: Length // Returns the string with leading and trailing spaces removed //**************************************************************************************** Label14a.Text = "Example #14: Length "; string strLengthExample = " ABC "; // Return Uppercase Label14.Text = strLengthExample.Length.ToString(); } }
Example Connection String:
<connectionstrings> <add name="Northwind_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> </connectionstrings>
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console