Working with StringBuilder in C#

This Post will look in to several Methods provided with StringBuilder class. There are several ways to declare and initialize an instance of StringBuilder class.

// declare StringBuilder instance
StringBuilder sb;

// default constructor
sb = new StringBuilder();

// new stringbuilder with initial capacity 
sb = new StringBuilder(10);

// new stringbuilder with with a new string
sb = new StringBuilder("codingphobia.com");

// initial with initial capacity
// and max limit over capacity
sb = new StringBuilder(10, 100);

// new stringbuilder with with a new string
// and max limit over capacity
sb = new StringBuilder("codingphobia.com", 1000);

// new stringbuilder with new string
// start index to place string
// length of string
//  max limit over capacity
sb = new StringBuilder("codingphobia.com", 10, 16, 1000);

Here are some of the built-in methods

// append string to stringbuilder
Console.WriteLine(sb.Append("First"));

// get current capacity
Console.WriteLine("current string capacity: " + sb.Capacity);

// compare string with stringbuilder
Console.WriteLine(sb.Equals("First"));

// get the hash code for the string builder
Console.WriteLine(sb.GetHashCode());

// get the type for the object
Console.WriteLine(sb.GetType());

// get the length for the string
Console.WriteLine(sb.Length);

// gt the capacity for stringbuilder
Console.WriteLine(sb.Capacity);

// gt the max capacity for stringbuilder
Console.WriteLine(sb.MaxCapacity);

// get the string anfd write to console
Console.WriteLine(sb);

// insert char at specific index
Console.WriteLine(sb.Insert(2, '-'));

// replace a char at specific index
Console.WriteLine(sb.Replace('-', ' '));

// remove a char from specific index
Console.WriteLine(sb.Remove(2, 1));

Following methods allows us to extend our stringbuilder with appending value at the end.

// Add string at the end of stringbuilder
sb.Append("new string");

// Add string with end of line at the end of stringbuilder
sb.AppendLine("new string");

// Add string at the end of stringbuilder, 
// with information about the format etc
sb.AppendFormat(new CultureInfo("en-US"),
                "# # # ### ##", new char[] { 'A', 'S', 'D', 'F' });

Following methods show how can we extend our string builder using insert method which has 18 overloads.
most common are as follows

 // insert char at 0 index 
 sb.Insert(0, '0');

 // insert bool at 0 index
 sb.Insert(0, true);

 // insert char Array at 0 index
sb.Insert(0, new char[] { '0', '1' });

// insert double value at 0 index
sb.Insert(0, 10.00);

// insert decimal value at 0 index
sb.Insert(0, 10);

// insert char Array value, from  index 2 to 3,
// at index 0 of string builder
sb.Insert(0, new char[] { '1', '2', '3', '4' }, 1, 1);

// insert string at 0 index
sb.Insert(0, "a String");

Passing indefinite parameters as Method arguments in C#

One might have a situation where he or she needs to pass a number of variables as arguments at runtime. When we are sure of the parameters required, we can declare them in method signature, like

         public static void PrintMessage(string message, int times)
        {
            for (int i = 0; i < times; i++)
                Console.WriteLine(message);
        }

In the above example we know that we need a message as string and an int (times) which would be used to display the message a number of times. What if we do not know how many variables of some type or different types are required at compile time, like print a number of messages, together or message or names . Here is params keyword which will gracefully tackle this situation. If we need list of argument of particular type, like string

         public static void PringtMessages(params string[] stringsList)
        {
            foreach (string s in stringsList) { }   
            // your logic here
        }

        // this is how to call
        string[] messages = new string[] { "Message1", "Message2" };
        PringtMessages(messages);

This is how, if we a need to pass different type of objects as indefinite number of arguments

        public static void PrintMessageWithID(params object[] list)
        {
            foreach (string s in list) { } 
            // your logic here
        }
        object[] messageAndId = new object[] { "Message1", 1,  "Message2", 2 };
        PrintMessageWithID(messageAndId);