C-SHARP - 4.9 Stringbuilder (Demo)
We now find ourselves on Visual Studio in order to put into practice what we have just seen in the previous video. We are going to have fun with the notion of Stringbuilder, let's start by creating a string builder type object, namely Stringbuilder. We will type var, we will then call it builder = new StringBuilder();. We said that the StringBuilder class was contained in the System.Text class, so that's why it's underlined here, it doesn't matter the class. So you right-click, you click here and you import the System.text class. Ok, now that we have our StringBuilder, let's explore some methods to build strings. You type the variable name builder and you put a dot to point to methods. And as you can see, we have for example several classic Append Append methods, AppendFormat, AppendJoin and AppendLine(). Let's take a look at the classic Append method. As you can see here, the Append method is an overloaded method. In the Stringbuilder class, there are other Append methods with a number of different parameters and types. There are 26 in total, here we have the one that takes a boolean but it can take a string, a char etc I will click on the Append and Visual Studio will redirect us to the StringBuilder class which contains all the methods that we can use. You see ? We can have a string, a double, an int, an array of char etc. So let's use for example the one that takes an array of characters. Either it takes a char and the value it must repeat as a parameter. So I would like the char * to be repeated ten times. So I put my star character comma as well as the number of times I want it to repeat so here I put 10. Then I do a Console.WriteLine() from my builder. I run the program and you see? We have our ten stars that are displayed. We will now create an empty line in our builder with the AppendLine() method And after that, I would like to display a Welcome header so to do that I do a builder.Append() again and al inside my parentheses I put my Welcome channel there. I'm going to copy and paste these lines to have a nice header in the output window. If I now run the program. Well you see? We have created a beautiful head very easily and quickly. Actually, StringBuilders are also meant to create some beautiful things. This is indeed string manipulation. With Stringbuilders, and another notion that we will see in this course. We can create random word generators by inserting it into our StringBuilder. Now let's look at other methods on our StringBuilder. Previously in the video, we saw that we could use the Replace() method. Same principle as for Strings, it allows you to replace a character, a string, etc. with something else. Now imagine that instead of putting stars here, I want to change to dashes. I could very well change it directly here, but I want you to work with the Replace method with a small algorithm. I'm going to ask the user to choose if they want to change the stars to dashes. Yes, yes then we change otherwise we send back No, I leave it like that. So I'm going to do a Console.WriteLine() to tell the user Do you want to change the * to -? (type yes to replace, no to leave) Then I will create a choice variable which will contain what the user will answer so I assign the Console.ReadLine() method. Then I make a condition, if choice is == a yes then we replace the stars with dashes. And inside my braces, I make builder pointing to my Replace() method the first parameter is the element we want to replace so here the star and the 2nd is the element with which we want the replace either dashes. We are not obliged to do an else but I am going to do it anyway and I am going to display No, I leave it like that. And at the end we display the builder with the Console.WriteLine() Ok now, if I run. Do you want to change the * to -? (Type yes to replace, no to leave). I type no to test if everything works, I enter hop it's good? It works if we put no, we have our stars displayed. Now if I raise and enter yes what happens? Well you see? We have all our stars which have been replaced by dashes. Here the user has typed yes so the if condition is true, we execute the Replace() method in order to replace what we put as the first parameter. Let's move on to the Remove() method, this method takes as a parameter an index and the number of elements it will remove. For example, we want to start at index 0 and we want to delete the first 10 elements. Before the Console.WriteLine() I make a builder pointing to the Remove() method in parentheses I put the index zero and the number of elements I want to delete is 10. Ok, if I run the program let's see what happens happens. So as we can see, the top line has been completely deleted, if I change I put 5 and I restart it deletes my 5 elements. We can also use the Insert method to insert things into our string. It takes as a parameter the index at which we want to insert our element and the 2nd parameter to be the element we want to insert. Here we have deleted the first 5 elements and I now want to insert more. So just after this line I do a builder.Insert() in the parentheses l index do I want to start inserting, here it is 0 and the element I want to insert is plus. So here I put a . If I run the program. You see I inserted my plus but I didn't get the result I wanted. In reality here, he is told to inject only one more and not 5 to complete the line. This is why we have this result. So here, you have to do what, you have to insert a string that will repeat 5 times as we did above with the append. Either we do a new string() parentheses with the character we want to add and the number of times is 5. Now if I run the program, well we have the expected result. To recap, here we create our string constructor. Then we added it with the append method of stars as well as welcome and the AppendLine() method to add empty story lines to make it more aesthetic. We made a small algorithm to ask the user if he wants to change the stars into dashes thanks to the Replace method. Afterwards, with the Remove() method, a number of elements given as a parameter have been removed from the beginning of the chain. And to finish, we inserted pluses from the beginning of the string. So you see that the string builder, Stringbuilder makes it easy to edit strings. Also, it's faster than manipulating strings with lots of operations on them. At each operation that we perform when manipulating strings, we create a String object in memory and return it. If now, we have a lot of string manipulation operations so the memory cost will be expensive. It is therefore preferable to use string builders so that our programs are more efficient and optimized. Also note that when creating a StringBuilder, it is quite possible to create a starting string. Here in the parentheses, we can add the string Bonjour à tous and won't prevent the rest of the manipulations we do just after. Let's look at the result. Now it is interesting to see that after all these manipulations, the first part of Bonjour à tous is deleted. More are there instead. And our stars have been replaced by dashes. So it's really easier to manipulate strings with the StringBuilder. Little reminder, as seen in the previous video. The only constraint is that it is not possible to search for string constructors. If we type builder dot here we find all the methods we can use with the Stringbuilders and unfortunately we don't have any that allows us to search for the index of a character or a string. However, it is possible to access individual characters in the Stringbuilder, by taking our builder and putting square brackets in it. We do as if to retrieve an element from an array, here we want to recover the first character so we put 0 brackets here I'm just going to add a string 1st char: that I'm going to concatenate and if I run the program You see? 1st char: is equivalent to a d here. Last thing before finishing, if we look a little more at the signature of our methods. Here we are returning a string constructor, Stringbuilder and not an empty method. Which means we can chain these append() methods and make our code cleaner. In reality, on each operation, we are returned our updated string constructor. Either we can directly point each time to the update. No need to recall our variable since it is automatically returned. So here, we can remove the semicolons and these builder variables in order to chain them all together. And basically, each time the method will apply to what a method will return. And you see that definitely makes our code more readable. And the same for this one. I can also chain them together. Either we now have a more pleasant and much cleaner code. In any case I hope this video has you more and that everything has been clear for you! See you next time in a new video. we can remove the semicolons and these builder variables in order to chain them all together. And basically, each time the method will apply to what a method will return. And you see that definitely makes our code more readable. And the same for this one. I can also chain them together.