VB.NET ASP.NET StringBuilder Source Code Example

Premium .NET Training Video Tutorials


Other Premium Videos Tutorials


VB.NET ASP.NET StringBuilder Source Code Example

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 StringBuilder 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 StringBuilder
  5. Click on copy code in code below to copy code into web form StringBuilder.aspx
  6. Click on copy code in second set of code below to copy code into code-behind StringBuilder.aspx.vb
  7. Right-click on StringBuilder.aspx in solution explorer and select view in browser

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

 XML |  copy code |? 
01
02
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="StringBuilder.aspx.vb" Inherits="LanguageBasics_StringBuilder" %>
03
 
04
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05
 
06
<html xmlns="http://www.w3.org/1999/xhtml">
07
<head runat="server">
08
    <title></title>
09
</head>
10
<body>
11
    <form id="form1" runat="server">
12
    <div>
13
        <asp:Label ID="Label1a" runat="server" Text="Label"></asp:Label><br />
14
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /><br />
15
        <asp:Label ID="Label2a" runat="server" Text="Label"></asp:Label><br />
16
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br /><br />
17
        <asp:Label ID="Label3a" runat="server" Text="Label"></asp:Label><br />
18
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br /><br />
19
        <asp:Label ID="Label4a" runat="server" Text="Label"></asp:Label><br />
20
        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br /><br />
21
        <asp:Label ID="Label5a" runat="server" Text="Label"></asp:Label><br />
22
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br /><br />
23
 
24
    </div>
25
    </form>
26
</body>
27
</html>
28
 
29
 
30



Step 2: Click on Copy Code to Cut-n-paste code into StringBuilder.aspx.vb
 Visual Basic |  copy code |? 
001
002
 
003
Imports System.Text
004
Imports System
005
Imports System.Text.StringBuilder
006
Partial Class LanguageBasics_StringBuilder
007
    Inherits System.Web.UI.Page
008
 
009
 
010
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
011
 
012
 
013
        '****************************************************************************************
014
        ' Example #1: StringBuilder Append(string) - most common way stringbuilder is used
015
        ' Adds the specified string or string representation of the specified value to the end 
016
        ' of the string
017
        '****************************************************************************************
018
 
019
 
020
        Dim sb As New StringBuilder
021
 
022
        Label1a.Text = "Example #1: Simple StringBuilder Append(string) "
023
        sb.Append("First Line")
024
        sb.Append("<br />")
025
        sb.Append("Second Line")
026
 
027
        Label1.Text = sb.ToString()
028
 
029
 
030
        'Append(string, startIndex, count)
031
        '****************************************************************************************
032
        ' Example #2: StringBuilder Append(string, startIndex, count) 
033
        ' Adds a substring of the specified string, starting with the specified
034
        ' position and having the specified length, to the end of the string
035
        '****************************************************************************************
036
 
037
 
038
        Dim sb2 As New StringBuilder
039
        Label2a.Text = "Example #2: StringBuilder Append(string, startIndex, count)"
040
        sb2.Append("The dog and cat ")
041
        sb2.Append("love to fight and play", 0, 13) 'take love to fight and add to end of prev string
042
        Label2.Text = sb2.ToString()
043
 
044
 
045
 
046
        'Insert(index, string[, count])
047
        '****************************************************************************************
048
        ' Example #3: StringBuilder Insert(index, string[, count]) 
049
        ' Inserts the specified string or a string representation of the specified
050
        ' value at the specified position in the string the specified number of 
051
        ' times. If the count is ommitted, a single copy of the string is inserted.
052
        '****************************************************************************************
053
 
054
 
055
        Dim sb3 As New StringBuilder
056
        Label3a.Text = "Example #3: Insert(index, string[, count])"
057
 
058
        sb3.Append("This is the initial sentence")
059
        sb3.Insert(20, "and modified ", 2)
060
        Label3.Text = sb3.ToString()
061
 
062
 
063
 
064
        'Remove(startIndex,count)
065
        '****************************************************************************************
066
        ' Example #4: StringBuilder Remove(startIndex,count) 
067
        ' Removes the specified number of characters from the string starting at
068
        ' the specified position
069
        '****************************************************************************************
070
 
071
 
072
        Dim sb4 As New StringBuilder
073
        Label4a.Text = "Example #4: StringBuilder Remove(startIndex,count)"
074
 
075
        sb4.Append("This is the initial sentence")
076
        sb4.Remove(20, 8)
077
        Label4.Text = sb4.ToString()
078
 
079
 
080
 
081
        'Replace(oldString,newString [,startIndex][,count])
082
        '****************************************************************************************
083
        ' Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count])
084
        ' Replaces all occurences of the old string with the new string starting
085
        ' at the specified position and continuing for the specified number of characters.
086
        ' If you omit startIndex, it starts at beginning
087
        ' If you omit count, it does entire string
088
        '****************************************************************************************
089
 
090
        Dim sb5 As New StringBuilder
091
        Label5a.Text = "Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count])"
092
 
093
 
094
        sb5.Append("This is the initial sentence")
095
        sb5.Replace("initial", "new")
096
        Label5.Text = sb5.ToString()
097
 
098
 
099
 
100
    End Sub
101
End Class
102
 
103
 
104
 
105
 
106

Step 3: Click on Copy Code to Cut-n-paste code into web.config right after the appSettings section
 XML |  copy code |? 
1
2
<connectionStrings>
3
    <add name="Northwind_ConnectionString"
4
      connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" />
5
    <add name="Pubs_ConnectionString"
6
      connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" />
7
  </connectionStrings>
8

Related posts:

  1. C# ASP.NET StringBuilder Source Code Example C# ASP.NET StringBuilder Source Code Example...
  2. VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
  3. C# StringBuilder Source Code Example C# StringBuilder Source Code Example...
  4. VB.NET ASP.NET String.Format DateTime Source Code Example VB.NET ASP.NET String.Format DateTime Source Code Example...
  5. C# ASP.NET String.Format DateTime Source Code Example C# ASP.NET String.Format DateTime Source Code Example...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

This blog uses the cross-linker plugin developed by Web-Developers.Net