C# StringBuilder Source Code Example

Get 5 Hours of FREE PREMIUM Videos:

LearnVisualStudio.NET Free Preview


LearnVisualStudio.NET Free Preview: Declaring Variables and Assigning Values

I am a lifetime member of LearnVisualStudio.net and a Premium Plus member of dotNetVideos.net.

LearnVisualStudio.net is awesome because it grows in value each year as more videos are added.

dotNetVideos.net is also great because it focuses a lot on MS Certifications and practical interview questions.


- Wade Harvey (IdealProgrammer.com)

LearnVisualStudio Premium Videos

Visual C# 2010 for Absolute Beginners C# for Absolute Beginners C# for Absolute Beginners Study Guide Visual Basic for Absolute Beginners Visual Basic Express Edition Study Guide
Visual Basic 9.0 Language Enhancements Entity Framework Linq To Sql Windows Phone 7 Development Unit Testing
ASP.NET MVC 3 (In Progress) ASP.NET MVC Hands On Project ASP.NET For Absolute Beginners Visual Web Developer C# Study Guide Visual Web Developer VB.NET Study Guide
ASP.NET Controls Series ASP.NET AJAX ASP.NET Architecture Series ASP.NET Server Controls Silverlight 4.0
Windows Presentation Foundation Windows Forms Controls Visual Studio 2010 New Features Visual Studio Team System 2008 Visual Studio Team System Features
Getting Started With Sql Server Express Edition


dotNetVideos Premium Video Catalog

Interview Questions & Answers Windows AzureASP.NET MVC 4Windows Phone 7 (3 Videos)Visual Studio 2011 Application Lifecycle Management
LightSwitchHTML 5SOLIDVisual Studio Add-OnsVisual Studio 2010 ALM Lab Management
ASP.NET 4 Deep Dive Microsoft Enterprise Library 5 with ASP.NET 4 (10 Videos)Virtualization Techniques for Developer (5 Videos)Test Series - 70-519: Designing and Developing Web Applications Using Microsoft .NET Framework 4 (20+ Videos)
Test Series - 70-519: Supplemental70-513: Test Series (MCTS) Windows Communication Foundation 4 (20+ Videos)70-515:Web Applications Development with Microsoft .NET Framework 4 (20+ Videos)Test Series - 70-516: Accessing Data with Microsoft .NET Framework 4 (20+ Videos)Test Series: 70-432: Microsoft SQL Server 2008, Implementation and Maintenance (20+ Videos)
Test Series: 70-433: SQL Server AdministrationFrom Novice To Professional - C# (25 Videos)From Novice To Professional - VB.NET (25 Videos)




Premium (Not Free) Video Tutorials

Free Video Tutorials & Free Tools

StringBuilder Source Code Example

Purpose: – Illustrates using and in C-Sharp.

Prerequistes:

  1. Install C# (Express or Standard Edition)
  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:

  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build your own library of syntax examples by using same project over and over and just adding new classes to it.

Instructions:

  1. Use C# 2008 (express or standard)
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select C-Sharp for Language
    • name of project could be CSharp_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 Class named clsStringBuilder to LanguageBasics folder
    • Right-click LanguageBasics folder;
    • add new item;
    • Select Class
    • Class name could be clsStringBuilder
  5. Click on copy code in code below to copy code into class clsStringBuilder.cs
  6. Click on copy code in second set of code below to copy code into Program.cs
  7. Click on green arrow in toolbar in VS or press F5 to run program.

Step 1: Click on Copy Code to Cut-n-paste code into clsStringBuilder.cs

 C# |  copy code |? 
using System.Text;
using System;
 
public class clsStringBuilder
{
 
    public void 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) ");
        StringBuilder sb = new StringBuilder();
        sb.Append("First Line");
        sb.Append("\n");
        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)");
        StringBuilder sb2 = 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])");
        StringBuilder sb3 = 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)");
        StringBuilder sb4 = 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])");
        StringBuilder sb5 = 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();
 
    }
 
 
}
 


Step 2: Click on Copy Code to Cut-n-paste code into Program.cs
 C# |  copy code |? 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CSharp_Syntax
{
    class Program
    {
        static void Main(string[] args)
        {
            //LanguageBasics
 
 
            clsStringBuilder myStringBuilder = new clsStringBuilder();
            myStringBuilder.Main();
 
 
        }
    }
}
 
 
 


Step 3: Click on green arrow in toolbar in VS or press F5 to run program.

Related posts:

  1. VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
  2. C# ASP.NET String.Format DateTime Source Code Example C# ASP.NET String.Format DateTime Source Code Example...
  3. VB.NET ASP.NET String.Format DateTime Source Code Example VB.NET ASP.NET String.Format DateTime Source Code Example...
  4. C# String.Format DateTime Source Code Example C# String.Format DateTime Source Code Example...
  5. C# Sql Command Delete Statement Source Code Example C# Source Code Example that illustrates using Sql Command Delete...

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!

Powered by WP Hashcash

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