In Part 1 of this series, C# Strings, I discussed basics of strings and some string operations such as adding, removing, inserting, comparing, cloning, and replacing strings. In this part, I will discuss some remaining methods of the String class and how to use them. Some of these methods are Format, Trim, TrimEnd, PadLeft, and PadRight.After discussing these methods, I will discuss the StringBuilder class.Initializing a StringBefore I discuss remaining (left from Part 1) methods of String class, let's see different ways to construct a string.Actually String class provides eight-overloaded form of constructor methods. These constructors are described in Table 1.
Table 1: The String class constructorsFormatting StringsYou can use the Format method to create formatted strings and concatenate multiple strings representing multiple objects. The Format method automatically converts any passed object into a string.For example, the following code uses integer, floating number and string values and format them into a string using the Format method.Listing 1: Using Format method to format a stringint val = 7;string name = "Mr. John";float num = 45.06f;string str = String.Format ("Days Left : {0}. Current DataTime: {1:u}. \n String: {2}, Float: {3}" , val, DateTime.Now, name, num);Console.WriteLine(str);The output of Listing 1 is shown Figure 1.
String str = " C# ";Console.WriteLine("Hello{0}World!", str);
string str1 = "My String";Console.WriteLine(str1.PadLeft(20, '-'));string str2 = "My String";Console.WriteLine(str2.PadRight(20, '-'));
using
Table 3. The StringBuilder class methods.Using StringBuilder properties and methods is pretty simple. Listing 4 uses StringBuilder class to append, insert, remove and replace characters of a string.Listing 4. Using StringBuilder class to append, add, replace and remove charactersStringBuilder builder = new StringBuilder("Hello C# World!", 20);StringBuilder builder = new StringBuilder("Hello C# World!", 20);int cap = builder.EnsureCapacity(55);builder.Append(". This is a class test.");Console.WriteLine(builder);builder.Insert(26," String Builder");Console.WriteLine(builder);builder.Remove(5, 9);Console.WriteLine(builder);builder.Replace('!', '?');Console.WriteLine(builder);Console.WriteLine("Length of string is:" + builder.Length.ToString() );Console.WriteLine("Capacity of string is:" + builder.Capacity.ToString() );The output of Listing 4 is shown in Figure 3.
Performance Note: Whenever you need to do multiple (say more than 5) string operations such as concatenating or adding strings five times to build a large string, it is recommended to use the StringBuilder class and its methods.About Source CodeDownlaod and unzip the attached cs files. Create a Windows console application using VS.NET and type the code. If you're compiling code from command line, compile cs files with no changes. Make sure you add reference to the System.Text references when compiling StringBuilder sample.
SummaryIn this article, I discussed some methods of String, basics of StringBuilder class and how to use StringBuilder class to add, insert, append and remove items from strings. In part 3 of this series, I convert part 1 and part 2 articles to VB.NET.