c sharp

C# String Builder

Anyone with a programming background knows that once the string is created, it cannot be changed, i.e. strings are immutable variables. Whenever we make changes to any string, a memory heap is used to create a new string object instead of changing the original one. You can say that this object may be a replica of an original object and has a different memory location. Thus, changing the replica doesn’t make changes to the original string. When it comes to performing different operations on a string, we have to create many new objects, which will be inefficient.

Here comes the String Builder class of C# for our service to avoid creating repeated objects for the changing of any string value by dynamically increasing its value. Let’s see how it works in C#. Let’s get started with this article implementation on Ubuntu 20.04 with the C# file creation in the terminal shell. For this, try using the “touch” query on the shell.

Example 01: String Append

Within our first example, we will see how a string builder class object can be used to append more strings at the end of a string without creating new objects. To open this newly created “builder.cs” C# file, we have used the Ubuntu 20.04 text editor built-in application. We will be starting with using the “System” library, which has been a must in every C# code. After this, we have been utilizing the “System.Text” class to use the string values in the script. We have started our program with the new “test” class creation with the keyword “class”. This class contains a single built-in function i.e. main() driver function.

Within this driver method, we have created an object “obj” of string builder class by using the traditional method for creating objects and added a string “Ubuntu” into it and specified the length 30, i.e. length can be expanded as it is a dynamic object. We have been using the “obj” object to class the function “AppendLine” of the “string builder” class to append the string “20.04” at the end of an original string with the line break. After this, we have used this object to append two string values again using the “Append” method without a line break, i.e. will be appended at a new line. The Console.WriteLine() function is here to display the updated string value using the object “obj” of the string builder class. Let’s save our code with Ctrl+S and exit the file with Ctrl+X.

You need to use the C# compiler “mcs” that is already configured in your system to compile your code file. After the code has been compiled, it will create an “exe” file at the same location where your c# file is located. Use the “exe” file in the “mono” command to run the compiled code. After running it, we have got the shown-below result. You can see that the string 20.04 has been appended at the end of “Ubuntu” with a line break. The next two strings have been appended to start a new line.

Example 02: String Format

The string builder has a method “AppendFormat” to let you display your specified string in a particular format of your choice. Let’s take a look at this function of string builder within this illustration. We have been using the same code structure as in the above example. Created an object “obj” of string builder class and assigned “Value:” as a string value to it. The object “obj” is used to call the “AppendFormat” function to append the given value into a specified format. We have given it the value 14 to be formatted in the decimal type format with the “$” sign—the Console.WriteLine() function uses the object to show the formatted string.

After this script accumulation and execution, we got the formatted string on our screen.

Example 03: String Insert

The insertion operation is a little different from append as the “append” function only inserts at the last of any string. The string builder class came up with the “Insert” function to insert any string value at any location of an original string. So, we have been using the same structure of the code and created an object “obj” of a string builder class first and created a string “Ubuntu” with this object. This object has been utilized to call the Insert function of the StringBuilder class to insert the string “20.04” at the specific location, i.e. at the index 6 of the original string. The structure of using this function is shown below—the Console.WriteLine() function displays the changes made to the original string via the object “obj”.

After compiling and executing our updated C# file, we got the output displayed below. As index 6 is the next to last character of an original string, thus it inserts at the last.

You want to insert the specified string at the index 3 of an original string. So, you have to specify “3” in the Insert() function during the function call, as shown below. The rest of your code will stay untouched. We will be saving this code to execute it after that.

After compiling our updated code and running it on the shell, we got the output presented below. The string “20.04” has been inserted in the mid of the original string “Ubuntu” as the index “3” is after the “Ubu” of the original string.

Example 04: String Remove

The string builder class provides the “Remove” function to completely remove the specific substring from the original one. So, we used the new string while creating an object “obj” and used the “Remove” function to remove the next 6 characters from the index 6 of the original string. The first parameter, “6”, indicates the index number, and the next indicates the number of characters to be removed. Let’s check this code on execution after saving it.

After the code compilation and running, the original string “Ubuntu -~!@ 20.04” has been converted to “Ubuntu 20.04” after its “-~!@” sub-part has been trimmed off or removed.

Example 05: String Replace

Just like removing, we can also replace the specific substring with a new substring within the original string with the “Replace” function of the string builder class. In the below example, we have utilized the Replace function to replace “-~!@” from the original string with the space using the object “obj”.

After running the compiled file, we have been presented with the updated string “Ubuntu 20.04” on our shell screen.

Conclusion

We have started this article with an explanation of utilizing the “String Builder” class needed in C# programming. We have discussed how a string builder class is a big help in making changes to any string value in programming. We have tried some simple and explained examples to append, insert, remove, replace, and change the format of a string utilizing the simple built-in function of a string builder class. We have thoroughly explained the uses of the Append, AppendLine, Insert, Remove, Replace, and AppendFormat functions of the string builder class.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.