From the course: Learning C#

Using StringBuilder - C# Tutorial

From the course: Learning C#

Using StringBuilder

- [Instructor] Building strings out of other strings is another very common operation in a variety of programming scenarios and the C# StringBuilder class aims to make this both easier to accomplish and more efficient. In .NET, the string object is immutable. When you call functions that make changes to strings, what you're actually doing is you're creating a copy of the string behind the scenes, which requires allocating the memory for the new string each time. And doing this over and over again can be a drag on performance and memory usage, which you can help avoid by using the StringBuilder. So let's go ahead and open up the sample code in the Builder folder. And the StringBuilder is located in the System.Text namespace, so that's why I'm including this here. So what we're going to do is start off by creating a StringBuilder and the way that we do that is I'll just declare the variable of type StringBuilder. And I'll call it sb. And I'll make a new StringBuilder. Now, it's not required, but if I want to, I can specify both an initial string content, as well as a maximum string capacity. So I'll make that 200. So once we've created the StringBuilder, we can start putting content into it and we can inspect its properties, like the current capacity and the current length of the string content. So let's go ahead and try that out. So I'll write out using my string interpolation feature that we learned about earlier in the chapter. So I'll print out the capacity. And that's going to be sb.Capacity. And Length is going to be sb.Length. So let's go ahead and try what we have so far. So I'll just bring up the terminal. And let's go ahead and run. And we can see that the capacity is 200 and the length is 15 because of this initial string that I put in there. So far, so good. Let's go ahead and close the terminal. There are multiple ways to add and manipulate the string content in a StringBuilder. The easiest way is to just use the append function to add string content to the builder. So for example, I'll write sb.Append and I'll just add the text I want, so I'll just put in the quick brown fox and I'll put in another line after that. Jumps over the lazy dog. And actually, I'm just going to make that jumped. There we go. And we can also add line endings using AppendLine, so I'll call AppendLine. That'll give me a carriage return. There's also an append format function, which we can use to append a formatting string in just one step instead of just creating a formatted string and then appending it. So I'll call sb.AppendFormat. And I'll give it a string and I'll say He did this and I'll use my index syntax for this one, the number of times. And you can see that if I scroll up, I've got this integer called jumpCount, so I'll put that in there. So that should be the number 10 when we see the output. And then I'll just put another blank line on there. The AppendJoin function will also condense the string class's join operation with an append so you can iterate over a sequence of values. So let's go ahead and try that one out. I'm going to call sb.Append and say oops, he also jumped over, and then I'm going to call sb.AppendJoin and I'm going to join using a comma and I'm going to pass in my array of other animals. So if we look up here, you can see that I've got these other animals up here, goats, cats and pigs. So I'll pass in my animals array. So that's going to join that into the StringBuilder. We can also modify the content of the StringBuilder using the replace function, just like we would a regular string. So for example, I can call sb.Replace and I can replace fox with cat. And we can insert string content at any index. So I'll call sb.Insert and I'll insert right at the beginning at index zero. And I'll pass in This is the. And then one more time, let's print out the capacity and string length after we've done all this work. So I'll copy this line here and paste it down here. And then finally, let's convert the StringBuilder's content to a final string. So I'll write Console.WriteLine and I'll call sb.ToString. And that will take all the content that's in the StringBuilder and serialize it into a final string. All right, so that's a lot of code that we've added there. So let's run the updated example. So we'll save and let's bring up our terminal. All right, let's run this. So we can see in the output, so here's the initial capacity and length. And you can see how the capacity and length have grown as we've done the work. You can also see that here's the initial string content that I created. And then I've got my other, the quick brown fox jumped over the lazy dog. That's my append statement. He did this 10 times. That's the result of my AppendFormat string right here. Then we did he also jumped over goats, cats, pigs. That's the AppendJoin. We then did the replace with fox and we replaced it with cat. And then we inserted this is the at the beginning of the string and yeah, so then we did the capacity and we converted the final output to a string. So the StringBuilder is a really efficient way of working with strings. So if your code needs to do a lot of string concatenation and manipulation, it's really worth taking a look at the builder class and of course, there's a C# documentation link for this. So if you go to this link in the C# docs, you can read all about the StringBuilder class and play around with it, try some other features that we didn't get to explore here and get to know how StringBuilder works.

Contents