Progress Monitor for IdealProgrammer.com
Take Swift, Intelligent, Massive, Planned, Loving, Effective (S.I.M.P.L.E.) Action to Transform Self into Ideal
Skip Repetitive Navigational Links
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console
Please login
C# StringBuilder Source Code Example
Description:
Illustrates using StringBuilder and StringBuilder Methods in C-Sharp.
Example Class Main:
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(); } }
Example Class Program:
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(); } } }
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console