C# ASP.NET String Examples – String Examples ASP.NET C#

C# ASP.NET String Examples – String Examples ASP.NET C#

Purpose: – Illustrates using and in C-Sharp ASP.NET.

Prerequistes:

  1. Install Visual Web Developer 2008
  2. Install SQL Server Express
  3. Download Northwind and Pubs Databases
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express

Notes:

  • You can build your own library of syntax examples by using same web site over and over and just add new web forms to it.

Instructions:

  1. Use Visual Web Developer 2008
  2. Create new web site;
    • Click File/New Web Site
    • Select ASP.NET Website Template
    • Select C-Sharp for Language
    • name of Web Site could be CSharp_ASPNET_Syntax.
  3. Add New folder named “LanguageBasics”
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: LanguageBasics
  4. Add Web Form Named String to LanguageBasics folder
    • Right-click LanguageBasics folder;
    • add new item;
    • Select Web Form
    • Check place code behind in separate file
    • Web Form name could be String
  5. Click on copy code in code below to copy code into web form String.aspx
  6. Click on copy code in second set of code below to copy code into code-behind String.aspx.cs
  7. Right-click on String.aspx in solution explorer and select view in browser

Step 1: Click on Copy Code to Cut-n-paste code into String.aspx

< %@ 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>

Step 2: Click on Copy Code to Cut-n-paste code into SqlCommandUpdate.aspx.cs

 
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();
 
 
 
 
    }
}

Step 3: Click on Copy Code to Cut-n-paste code into web.config right after the appSettings section

<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>