VB.NET StringBuilder Source Code Example


Putting
the Pieces of .NET Together - 8 Part Video Series | ||||
| Introduction | ||||
| Videos 1-4 | 1. Servers (3 minutes) | 2. .NET Framework (10 minutes) | 3. Security (8 minutes) | 4. Monitoring Tools (10 minutes) |
| Videos 5-8 | 5. Web Servers (6 minutes) | 6. SQL Server (6 minutes) | 7. Software Develop Tools (10 minutes) | 8. Languages (2 minutes) |
| Download PDF | Putting the Pieces of .NET Together - 48-page PDF | |||
VB.NET StringBuilder Source Code Example
Purpose: – Illustrates using StringBuilder and StringBuilder Methods.
Prerequistes:
- Install Visual Basic (Express or Standard Edition)
- Install SQL Server Express
- Download Northwind and pubs Database
- Attach Northwind Database to Databases in Sql Express
- Attach pubs Database to Databases in Sql Express
Notes:
- Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
- You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Module1.vb
Instructions:
- Use Visual Basic 2008 Express or Standard Edition
- Create new project;
- Click File/New Project
- Select Console Application Template
- Select Visual Basic for Language
- name of project could be VBNET_Syntax.
- Add New folder named "Language-Basics"
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: Language-Basics
- Add Class Named clsStringFormatDateTime to Language-Basics folder
- Right-click Language-Basics folder;
- add new item;
- Select class
- Class name could be clsStringBuilder
- Click on copy code in code below to copy code into clsStringBuilder.vb
- Click on copy code in second set of code below to copy code into Module1.vb
- Click green arrow or press F5 to run program
Step 1: Use Copy Code to Cut-n-paste code into clsStringBuilder.vb
| Visual Basic | | copy code | | ? |
Imports System.Text |
Imports System |
Public Class clsStringBuilder |
Public Sub Main() |
'**************************************************************************************** |
' Example #1: StringBuilder Append(string) - most common way stringbuilder is used |
' Adds the specified string or string representation of the specified value to the end |
' of the string |
'**************************************************************************************** |
Console.WriteLine("Example #1: Simple StringBuilder Append(string) ") |
Dim sb As New StringBuilder |
sb.Append("First Line") |
sb.Append(vbCrLf) |
sb.Append("Second Line") |
Console.WriteLine(sb.ToString()) |
'write blank line to make output easier to read |
Console.WriteLine() |
'Append(string, startIndex, count) |
'**************************************************************************************** |
' Example #2: StringBuilder Append(string, startIndex, count) |
' Adds a substring of the specified string, starting with the specified |
' position and having the specified length, to the end of the string |
'**************************************************************************************** |
Console.WriteLine("Example #2: StringBuilder Append(string, startIndex, count)") |
Dim sb2 As New StringBuilder |
sb2.Append("The dog and cat ") |
sb2.Append("love to fight and play", 0, 13) 'take love to fight and add to end of prev string |
Console.WriteLine(sb2.ToString()) |
'write blank line to make output easier to read |
Console.WriteLine() |
'Insert(index, string[, count]) |
'**************************************************************************************** |
' Example #3: StringBuilder Insert(index, string[, count]) |
' Inserts the specified string or a string representation of the specified |
' value at the specified position in the string the specified number of |
' times. If the count is ommitted, a single copy of the string is inserted. |
'**************************************************************************************** |
Console.WriteLine("Example #3: Insert(index, string[, count])") |
Dim sb3 As New StringBuilder |
sb3.Append("This is the initial sentence") |
sb3.Insert(20, "and modified ", 2) |
Console.WriteLine(sb3.ToString()) |
'write blank line to make output easier to read |
Console.WriteLine() |
'Remove(startIndex,count) |
'**************************************************************************************** |
' Example #4: StringBuilder Remove(startIndex,count) |
' Removes the specified number of characters from the string starting at |
' the specified position |
'**************************************************************************************** |
Console.WriteLine("Example #4: StringBuilder Remove(startIndex,count)") |
Dim sb4 As New StringBuilder |
sb4.Append("This is the initial sentence") |
sb4.Remove(20, 8) |
Console.WriteLine(sb4.ToString()) |
'write blank line to make output easier to read |
Console.WriteLine() |
'Replace(oldString,newString [,startIndex][,count]) |
'**************************************************************************************** |
' Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count]) |
' Replaces all occurences of the old string with the new string starting |
' at the specified position and continuing for the specified number of characters. |
' If you omit startIndex, it starts at beginning |
' If you omit count, it does entire string |
'**************************************************************************************** |
Console.WriteLine("Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count])") |
Dim sb5 As New StringBuilder |
sb5.Append("This is the initial sentence") |
sb5.Replace("initial", "new") |
Console.WriteLine(sb5.ToString()) |
'write blank line to make output easier to read |
Console.WriteLine() |
'Prevent console from closing before you press enter |
Console.ReadLine() |
End Sub |
End Class |
Step 2: Use View Plain to Cut-n-paste code into Module1.vb
| Visual Basic | | copy code | | ? |
Module Module1 |
Sub Main() |
'***** Language-Basics ************* |
Dim myStringBuilder As New clsStringBuilder |
myStringBuilder.Main() |
End Sub |
End Module |
Related posts:
- C# StringBuilder Source Code Example C# StringBuilder Source Code Example...
- VB.NET ASP.NET StringBuilder Source Code Example VB.NET ASP.NET StringBuilder Source Code Example...
- C# ASP.NET StringBuilder Source Code Example C# ASP.NET StringBuilder Source Code Example...
- VB.NET String – String VB.NET VB.NET String - String VB.NET ...
- C# String Examples – String Examples C# C# String Examples - String Examples C#...
Related posts brought to you by Yet Another Related Posts Plugin.
