C# StringBuilder Source Code Example

"I tried to learn .NET by taking boot camp classes that cost me $6,000. After taking the classes, I discovered tons of free (and very cheap) videos on the internet created by experts in the field. I found the videos to be much better than the classrooms. The videos were presented by Microsoft presenters that knew the material backwards and forwards, and they were much cheaper and better than the classroom teachers. I have spent the last three years organizing and hunting down the videos for my own use and decided that all could benefit by sharing what I found." - Wade Harvey

Click Here for Free 24 hour pass to lynda.com.

I AM VERY EXCITED ABOUT THIS AMAZING NEW DISCOVERY

Over 40,000 Top-Notch Video Tutorials (or 3,452 hours) on Lynda.com

I had stumbled across Lynda.com many times before, but never stopped to try it out. I recently took the plunge and started using Lynda.com to help me improve my javascript skills (See Javascript Essential Training 2007 by Dori Smith). It has revolutionized my understanding of the language! I love the fact that the exercise files allow you to follow along in real time with what the instructor is saying. Learning by doing is an extremely powerful technique. I have to give the site an A+ for both quality and content.

I am planning on using Lynda Videos to help me improve in:

  1. HTML
  2. CSS
  3. Classic ASP
  4. .NET (included in ASP section)
  5. AJAX
  6. Silverlight (in Microsoft Section)
  7. SharePoint (in Microsoft Section)
  8. Web Design
  9. Blogging
  10. SEO
  11. and much more
You can learn more about Lynda.com by watching the 5-minute video at About Lynda.com. If you are only focusing on .NET, you are missing out on the "big picture" and on many of the underlying fundamentals! To be an Ideal Programmer, you need to have the largest knowledge base possible.

JavaScript tutorials



Looking for premium .NET Training Videos? The best premium .NET Videos that I have found are at Learn Visual Studio. Those videos are only about 50 cents per hour, as compared to $25 per hour that other sites charge!

LearnVisualStudio is currently having a 25% off sale ($50 savings) and giving away a free membership to TrainingSpot ($60 value) when LVS Lifetime Membership is purchased.


DiscountASP .NET hosting JavaScript tutorials


Limited Time Offer: Free Lifetime Membership to Progress Monitor ($24.95 value) when you purchase any membership at Learn Visual Studio, Lynda Videos or any hosting account at DiscountASP - just forward order confirmation to harvey007@sbcglobal.net

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

  |  copy code |? 
001
002
using System.Text;
003
using System;
004
 
005
public class clsStringBuilder
006
{
007
 
008
    public void Main()
009
    {
010
 
011
        //****************************************************************************************
012
        // Example #1: StringBuilder Append(string) - most common way stringbuilder is used
013
        // Adds the specified string or string representation of the specified value to the end 
014
        // of the string
015
        //****************************************************************************************
016
 
017
        Console.WriteLine("Example #1: Simple StringBuilder Append(string) ");
018
        StringBuilder sb = new StringBuilder();
019
        sb.Append("First Line");
020
        sb.Append("\n");
021
        sb.Append("Second Line");
022
 
023
        Console.WriteLine(sb.ToString());
024
 
025
        //write blank line to make output easier to read
026
        Console.WriteLine();
027
 
028
        //Append(string, startIndex, count)
029
        //****************************************************************************************
030
        // Example #2: StringBuilder Append(string, startIndex, count) 
031
        // Adds a substring of the specified string, starting with the specified
032
        // position and having the specified length, to the end of the string
033
        //****************************************************************************************
034
 
035
        Console.WriteLine("Example #2: StringBuilder Append(string, startIndex, count)");
036
        StringBuilder sb2 = new StringBuilder();
037
        sb2.Append("The dog and cat ");
038
        sb2.Append("love to fight and play", 0, 13);
039
        //take love to fight and add to end of prev string
040
        Console.WriteLine(sb2.ToString());
041
 
042
        //write blank line to make output easier to read
043
        Console.WriteLine();
044
 
045
        //Insert(index, string[, count])
046
        //****************************************************************************************
047
        // Example #3: StringBuilder Insert(index, string[, count]) 
048
        // Inserts the specified string or a string representation of the specified
049
        // value at the specified position in the string the specified number of 
050
        // times. If the count is ommitted, a single copy of the string is inserted.
051
        //****************************************************************************************
052
 
053
        Console.WriteLine("Example #3: Insert(index, string[, count])");
054
        StringBuilder sb3 = new StringBuilder();
055
 
056
        sb3.Append("This is the initial sentence");
057
        sb3.Insert(20, "and modified ", 2);
058
        Console.WriteLine(sb3.ToString());
059
 
060
        //write blank line to make output easier to read
061
        Console.WriteLine();
062
 
063
        //Remove(startIndex,count)
064
        //****************************************************************************************
065
        // Example #4: StringBuilder Remove(startIndex,count) 
066
        // Removes the specified number of characters from the string starting at
067
        // the specified position
068
        //****************************************************************************************
069
 
070
        Console.WriteLine("Example #4: StringBuilder Remove(startIndex,count)");
071
        StringBuilder sb4 = new StringBuilder();
072
 
073
        sb4.Append("This is the initial sentence");
074
        sb4.Remove(20, 8);
075
        Console.WriteLine(sb4.ToString());
076
 
077
        //write blank line to make output easier to read
078
        Console.WriteLine();
079
 
080
        //Replace(oldString,newString [,startIndex][,count])
081
        //****************************************************************************************
082
        // Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count])
083
        // Replaces all occurences of the old string with the new string starting
084
        // at the specified position and continuing for the specified number of characters.
085
        // If you omit startIndex, it starts at beginning
086
        // If you omit count, it does entire string
087
        //****************************************************************************************
088
 
089
        Console.WriteLine("Example #5: StringBuilder Replace(oldString,newString [,startIndex][,count])");
090
        StringBuilder sb5 = new StringBuilder();
091
 
092
        sb5.Append("This is the initial sentence");
093
        sb5.Replace("initial", "new");
094
        Console.WriteLine(sb5.ToString());
095
 
096
        //write blank line to make output easier to read
097
        Console.WriteLine();
098
 
099
 
100
 
101
 
102
 
103
 
104
 
105
        //Prevent console from closing before you press enter
106
        Console.ReadLine();
107
 
108
    }
109
 
110
 
111
}
112
 
113


Step 2: Click on Copy Code to Cut-n-paste code into Program.cs
  |  copy code |? 
01
02
using System;
03
using System.Collections.Generic;
04
using System.Linq;
05
using System.Text;
06
 
07
namespace CSharp_Syntax
08
{
09
    class Program
10
    {
11
        static void Main(string[] args)
12
        {
13
            //LanguageBasics
14
 
15
 
16
            clsStringBuilder myStringBuilder = new clsStringBuilder();
17
            myStringBuilder.Main();
18
 
19
 
20
        }
21
    }
22
}
23
 
24
 
25
 
26



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 StringBuilder Source Code Example C# ASP.NET StringBuilder Source Code Example...
  3. VB.NET ASP.NET StringBuilder Source Code Example VB.NET ASP.NET StringBuilder Source Code Example...
  4. C# String Examples – String Examples C# C# String Examples - String Examples C#...
  5. VB.NET String – String VB.NET VB.NET String - String VB.NET ...

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

http://idealprogrammer.com/home/idearcom/public_html/wp-content/plugins/wp-super-cache/