VB.NET ASP.NET String Examples – String Examples ASP.NET VB.NET

VB.NET ASP.NET String Examples – String Examples ASP.NET VB.NET

Purpose: – Illustrates using and in VB.NET 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 Visual Basic for Language
    • name of Web Site could be VBNET_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.vb
  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="VB" AutoEventWireup="false" CodeFile="String.aspx.vb" 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 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 />
        <asp :Label ID="Label15a" runat="server" Text="Label"></asp><br />
        <asp :Label ID="Label15" runat="server" Text="Label"></asp><br /><br />
    </div>
    </form>
</body>
</html>

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

 
 
Partial Class LanguageBasics_String
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        '****************************************************************************************
        ' 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 #15: Chars(index)
        '****************************************************************************************
 
 
        '****************************************************************************************
        ' Example #1: StartsWith(string)
        ' Returns a Boolean value that indicates if the string starts with the specified string. 
        '****************************************************************************************
 
        Label1a.Text = "Example #1: StartsWith(string) "
 
        Dim strStartsWithExample As String = "This is a test string"
 
        Label1.Text = strStartsWithExample.StartsWith("This") '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) "
 
        Dim strEndsWithExample As String = "This is a test string"
 
        Label2.Text = strEndsWithExample.EndsWith("string") '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]) "
 
        Dim strIndexOfExample As String = "This is a test string"
 
        Label3.Text = strIndexOfExample.IndexOf("is") '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]) "
 
        Dim strLastIndexOfExample As String = "This is a test string"
 
        Label4.Text = strLastIndexOfExample.LastIndexOf("is")  '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) "
 
        Dim strInsertExample As String = "This is a test string"
        Dim intIndex As Integer = 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]) "
 
        Dim strPadLeftExampleA As String = "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]) "
 
        Dim strPadRightExampleA As String = "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) "
 
        Dim strRemoveExample As String = "This is a test string"
        Dim intIndexRemove As Integer = 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) "
 
        Dim strReplaceExample As String = "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]) "
 
        Dim strSubstringExample As String = "This is a test string"
        Dim intIndexSubstring As Integer = 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"
 
        Dim strToLowerExample As String = "This is a test string"
 
        ' Return Lowercase
        Label11.Text = strToLowerExample.ToLower
 
 
        '****************************************************************************************
        ' Example #12: ToUpper
        ' Returns the string in Uppercase
        '****************************************************************************************
 
        Label12a.Text = "Example #12: ToUpper "
 
        Dim strToUpperExample As String = "This is a test string"
 
        ' Return Uppercase
        Label12.Text = strToUpperExample.ToUpper
 
 
 
        '****************************************************************************************
        ' Example #13: Trim
        ' Returns the string with leading and trailing spaces removed
        '****************************************************************************************
 
        Label13a.Text = "Example #13: Trim "
 
        Dim strTrimExample As String = "     ABC     "
 
        ' Return Uppercase
        Label13.Text = strTrimExample.Trim.Length
 
 
        '****************************************************************************************
        ' Example #14: Length
        ' Returns the string with leading and trailing spaces removed
        '****************************************************************************************
 
        Label14a.Text = "Example #14: Length "
 
        Dim strLengthExample As String = "     ABC     "
 
        ' Return Uppercase
        Label14.Text = strLengthExample.Length
 
        '****************************************************************************************
        ' Example #15: Chars(index)
        ' Gets the character at the specified index
        '****************************************************************************************
 
        Label15a.Text = "Example #15: Chars(index) "
 
        Dim strCharsExample As String = "ABC"
 
        ' Return Uppercase
        Label15.Text = strCharsExample.Chars(1)
 
 
 
    End Sub
End Class

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>