VB.NET StringBuilder Source Code Example
| .NET | .NET Programming (C# or VB) (Web or Desktop) | |||
| Key Supporting Languages | HTML | CSS | JavaScript | SQL Server |
| Access | AJAX | ASP | Blogging | ColdFusion |
| Expression Blend | mySQL | Perl | PHP | Ruby |
| SEO | SharePoint | Silverlight | Web Design | Web Dev |
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 | | ? |
| 001 | |
| 002 | Imports System.Text |
| 003 | Imports System |
| 004 | |
| 005 | Public Class clsStringBuilder |
| 006 | |
| 007 | Public Sub Main() |
| 008 | |
| 009 | '**************************************************************************************** |
| 010 | ' Example #1: StringBuilder Append(string) - most common way stringbuilder is used |
| 011 | ' Adds the specified string or string representation of the specified value to the end |
| 012 | ' of the string |
| 013 | '**************************************************************************************** |
| 014 | |
| 015 | Console.WriteLine("Example #1: Simple StringBuilder Append(string) ") |
| 016 | Dim sb As New StringBuilder |
| 017 | sb.Append("First Line") |
| 018 | sb.Append(vbCrLf) |
| 019 | sb.Append("Second Line") |
| 020 | |
| 021 | Console.WriteLine(sb.ToString()) |
| 022 | |
| 023 | 'write blank line to make output easier to read |
| 024 | Console.WriteLine() |
| 025 | |
| 026 | 'Append(string, startIndex, count) |
| 027 | '**************************************************************************************** |
| 028 | ' Example #2: StringBuilder Append(string, startIndex, count) |
| 029 | ' Adds a substring of the specified string, starting with the specified |
| 030 | ' position and having the specified length, to the end of the string |
| 031 | '**************************************************************************************** |
| 032 | |
| 033 | Console.WriteLine("Example #2: StringBuilder Append(string, startIndex, count)") |
| 034 | Dim sb2 As New StringBuilder |
| 035 | sb2.Append("The dog and cat ") |
| 036 | sb2.Append("love to fight and play", 0, 13) 'take love to fight and add to end of prev string |
| 037 | Console.WriteLine(sb2.ToString()) |
| 038 | |
| 039 | 'write blank line to make output easier to read |
| 040 | Console.WriteLine() |
| 041 | |
| 042 | 'Insert(index, string[, count]) |
| 043 | '**************************************************************************************** |
| 044 | ' Example #3: StringBuilder Insert(index, string[, count]) |
| 045 | ' Inserts the specified string or a string representation of the specified |
| 046 | ' value at the specified position in the string the specified number of |
| 047 | ' times. If the count is ommitted, a single copy of the string is inserted. |
| 048 | '**************************************************************************************** |
| 049 | |
| 050 | Console.WriteLine("Example #3: Insert(index, string[, count])") |
| 051 | Dim sb3 As New StringBuilder |
| 052 | |
| 053 | sb3.Append("This is the initial sentence") |
| 054 | sb3.Insert(20, "and modified ", 2) |
| 055 | Console.WriteLine(sb3.ToString()) |
| 056 | |
| 057 | 'write blank line to make output easier to read |
| 058 | Console.WriteLine() |
| 059 | |
| 060 | 'Remove(startIndex,count) |
| 061 | '**************************************************************************************** |
| 062 | ' Example #4: StringBuilder Remove(startIndex,count) |
| 063 | ' Removes the specified number of characters from the string starting at |
| 064 | ' the specified position |
| 065 | '**************************************************************************************** |
| 066 | |
| 067 | Console.WriteLine("Example #4: StringBuilder Remove(startIndex,count)") |
| 068 | Dim sb4 As New StringBuilder |
| 069 | |
| 070 | sb4.Append("This is the initial sentence") |
| 071 | sb4.Remove(20, 8) |
| 072 | Console.WriteLine(sb4.ToString()) |
| 073 | |
| 074 | 'write blank line to make output easier to read |
| 075 | Console.WriteLine() |
| 076 | |
| 077 | 'Replace(oldString,newString [,startIndex][,count]) |
| 078 | '**************************************************************************************** |
| 079 | ' Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count]) |
| 080 | ' Replaces all occurences of the old string with the new string starting |
| 081 | ' at the specified position and continuing for the specified number of characters. |
| 082 | ' If you omit startIndex, it starts at beginning |
| 083 | ' If you omit count, it does entire string |
| 084 | '**************************************************************************************** |
| 085 | |
| 086 | Console.WriteLine("Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count])") |
| 087 | Dim sb5 As New StringBuilder |
| 088 | |
| 089 | sb5.Append("This is the initial sentence") |
| 090 | sb5.Replace("initial", "new") |
| 091 | Console.WriteLine(sb5.ToString()) |
| 092 | |
| 093 | 'write blank line to make output easier to read |
| 094 | Console.WriteLine() |
| 095 | |
| 096 | |
| 097 | |
| 098 | |
| 099 | |
| 100 | |
| 101 | |
| 102 | 'Prevent console from closing before you press enter |
| 103 | Console.ReadLine() |
| 104 | |
| 105 | End Sub |
| 106 | |
| 107 | |
| 108 | End Class |
| 109 | |
| 110 |
Step 2: Use View Plain to Cut-n-paste code into Module1.vb
| Visual Basic | | copy code | | ? |
| 01 | |
| 02 | Module Module1 |
| 03 | |
| 04 | Sub Main() |
| 05 | '***** Language-Basics ************* |
| 06 | |
| 07 | Dim myStringBuilder As New clsStringBuilder |
| 08 | myStringBuilder.Main() |
| 09 | |
| 10 | |
| 11 | End Sub |
| 12 | |
| 13 | End Module |
| 14 | |
| 15 |
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.
