C-SHARP - 4.7 Advanced String (demo)
Hello everyone and welcome to this new video. We will now move on to practicing the theory we saw in the previous video. First, we saw the ToLower(), ToUpper and Trim() method. So here I am going to create a chain. I do string string = quotes and I type Hello World. Next, I'm going to do a Console.WriteLine of my string. Ok now, we will apply our methods on this chain. For this, I will make several Console.WriteLine with our chain that points to each method, the first ToLower colon and I add the variable with the method, for the second the same ToUpper() colon variable point the method and the 3rd Trim() too. Ok, I'm just going to add spaces here so we can see what the Trim method will do. And there, I will add a small front to display the original chain. I run the program. And you see, here we have our original channel. On the second line, we have a copy of our string in lowercase thanks to the ToLower() method. Then here, we have our string in uppercase thanks to the ToUpper() method and to finish here, we can clearly see that the spaces which were at the beginning, we were removed thanks to the Trim method. I also specify that the Trim method removes spaces only at the beginning and end of the string. Here in the middle, she doesn't care. Let's move on, I'll add a Console.WriteLine() to skip a line in console and I'll add a comment line to separate our code. In a second step, we saw that we could, if we wish, search for a character or a word in a string with the Indexof or Lastindexof method to start our search from the end. I'm going to create a new channel, I'm going to string s = for example I'm going to go to the gym Ok now, if I want to retrieve the index of the word gym, how am I going to do it? Here I am going to display it directly with Console.WriteLine() but it is quite possible to store in a variable what I am going to put in the parentheses. So here I am going to give my string then I call the indexof method and I put the word room in quotes as a parameter. If I run my program, it returns the index, the location of the first letter of my word. Now imagine, I type a word that is not in my string, I put here for example motorcycle. I restart the program, well it returns -1 because it did not find the word in my string. One can even make me better. Here I will make a condition so that when it does not find a word in the string, it displays me This word is not in the string , it is already more understandable than the -1. So here I do an if I give my string pointing to the indexof method with the word I'm looking for here it's motorcycle I'm just going to create a word variable quickly and assign it to motorcycle then is == a -1 i.e. it can't find it so you show me what's in the Console.WriteLine. Here I do a Console.WriteLine() This word is not in the string else which treats the cases where it finds the character or the word so I display Console.WriteLine This word is at the index colon the chain s.indexof() of the word. Here we could have put the word directly in quotes but we have to create a reusable program so imagine we change the word, we have to rewrite it everywhere each time whereas we just have to change it at the top and it is changed everywhere automatically. Or if we decide that it is now the user who types the word we will just have to replace the word here by the Console.ReadLine() function. I will delete a and now if I run the program. You see, it no longer returns -1 to me but well This word is not in the string . And if I change, I put a word in the string. I put here room. I restart the program, it displays me well This word is at index 21. I will now add to my chain here and afterwards I am going to go to the cinema . I purposely added a I'm going to test with the Lastindexof function to show you that it will give me the index of the 2nd Here I will do a Console.WriteLine() I will put The index is two points s.Lastindexof() and in parameter I put the word go. I run the program and you see it returns the index of the first letter of the 2nd letter to me because it starts searching from the end. The first is located at index 3 while the 2nd is located at index 47. And as I said in the previous video, we start searching from the end. However the index system remains the same. We give the index from the beginning but we browse from the end. Then, we saw that we could create a chain from a given chain. That is to say that from this string, for example contained in the variable s, we can copy part of it and store it in a variable with the Substring method, giving as a parameter the location of the o we want to start copying. Here for example I want to copy from and . You remember in the previous video, I had you to think about this. How can I copy from and knowing that I am not going to have fun counting the indexes one by one and especially if we have to create a reusable program. So do you have an idea? Don't we know a method allowing to recover the index of a character or a string that is passed to it as a parameter? Yes, you guessed it, it is the indexof() method. Here we are going to put in the parentheses of our Substring method the indexof method with the word from which we want to start because as you know, the indexof method returns the index of the string that we pass to it as a parameter so here we will return in the parentheses an index and then we can make our copy. So I'm going to create a copy variable which will store the Substring method which takes as a parameter the indexof method which will return the index of the word. Then I do a Console.WriteLine() and inside I put my copy variable. And if I run the program, we can see that the program returned what we wanted. The indexof function captures the index of the word and then the index was placed as a parameter in the substring method and the copy of the string we point to was created from it Which gives us the following string and I'm going to the cinema Another thing, we had said that the Substring method was overloaded, that is to say that in the class System, there is a 2nd method of the same name, however with more parameters. The particularity is that it limits copying. While the first copies everything to the end of the string. I'm going to take the channel we copied and I'm going to limit it by stopping before the cinema. For that, I'm going to do a Console.WriteLine() to do that quickly and display the result directly, inside I give it a copy pointing to the Substring() method. The first parameter is where we want to start copying. Me, I want to start from the beginning so I put the index 0 and the second is up to where I want to copy so here I want to copy up to the space that there is before cinema so what do I do here I do parentheses jy place copy pointing to the indexof function inside I put the word cinema If now I launch the program, we have good and I go to which is displayed Then, we had that there is a method which allowed to change a character or a sub-string present in a string. For example, I want to change all the a's in a string to e or else I have the string Jason is going to the gym and I want to change the first name Jason and replace it with Jibril. We have seen that it was the Replace method which made it possible to do this. I'm just going to put some comments and some Console.WriteLine() to skip lines in console so that it's more readable I'm going to create a tmp variable that will store the word aaaallllo with several a's and I'm going to create a copy of this string in replace all a with e. How do I do this, I will do a Console.WriteLine() inside I give my tmp string pointing to the Replace() method. It takes 2 things as parameters, the character or the word it wants to replace, either here the a and the 2nd by which it wants to replace it, i.e. e So now I am going to run the program to see the result. And there, we have all our a's that have been replaced by e's. I will now test for the 2nd example. I'm changing my channel here hop: Jason is going to the gym. Here the element I want to change is Jason and what I want to replace him with is Jibril. So I restart the program and we have replaced Jason with Jibril. Alright, let's move on. We saw in the previous video, two methods to see if a string is empty or not, so it returns a boolean True or False. True if it is empty and False if it is not. We said that the difference between the two c is that the first String.IsNullOrEmpty, it had to be strictly empty. While the 2nd, String.IsNullOrWhiteSpace if there is a space but nothing else then it is considered empty. So to test this, we're going to create an empty variable and we're going to store a space in it. Then we will do Console.WriteLine() and inside we will put the method String.IsNullOrEmpty. And this method takes a string as a parameter so we will put the empty variable to it. I launch the program and you see we have False which is returned to us because the string is not strictly empty it contains a space if now I delete this space and I restart. This time, it is strictly empty so we have true which is returned to us. Now I will try with the other String method. IsNullOrWhiteSpace, I put the space back and I run the program. Hop, you see we have true which is returned to us because it considers that the string is empty there is not the condition of strictly, simply the fact that the characters are absent. While there, if I put a character after the space. I launch the program and you see it? We have False because it is no longer empty. Now let's move on to the split method which allows you to split a string using a separator. And returns an array of string. To test this method, we will use the copy variable which contains the string and I will go to the cinema . I'm going to create a var type variable that we're going to call tab and we're going to assign it the copy variable pointing to the split method and having a space as a separator. Here why did I create a variable var? Because the split method returns us an array of strings and when we put the type var it deduces the type according to what we assign to it. And if we look here we can see that it is an array of strings. Now if I put the tab variable in a Console.WriteLine() what will happen? Let's test to see, presto, I launch the program and, you see? System.String brackets. Why do you have this result? Bah here basically we ask him to show us what the complete table is worth, it is therefore normal that the program sends us a. We are not asking it to display the elements of this array. So we know how to do this in C#, what should we do? One loop exactly. We will therefore do a for int i = 0 then i less than the number of elements in my array, i.e. length and i Then we do a Console.WriteLine and inside it is told to display tab of i. If we now launch the program, well we have all our elements displayed. The particularity here is that without the split method, if we had placed our string in an array. We would have the string directly displayed on one and the same line because this array would contain a single element, be the complete chain. On the other hand here, with the split method, we have separated each word of the string and therefore each word corresponds to an element of the array. And finally, let's move on to conversion. The conversion of a string to a number, we saw that in an old video so we did a reminder in the previous video so I'm not going to revive this notion. However, there is something new for you, it is to convert a number into a string thanks to the ToString() method which takes either no parameter or it takes a formatting string for example to transform an int into a monetary value in dollars for example which is impossible to make a into an int type because there are special characters the comma the dollar etc So here, we are going to create an int type variable that we are going to call price = a 399. And we are going to use 3 formats to display it then if you want to test with other formats you can go to the Microsoft documentation and test on your side. First of all, we are going to test the first one, the simple one so we make a Console. WriteLine() and inside we will take the variable that contains the price then we will point to the ToString() method Then I will copy and paste two except that for the other two I will add a format string for the 2 me I will put C which is the abbreviation of Currency to give a monetary value and the 3rd I will put C0 to avoid having zero more. I will launch the program and you will understand. So here as you can see, we first have the simple string conversion of our number. Then we have the conversion with a format string to give a monetary value with number and just below without. On the other hand, we have a small problem, we have a question mark. Basically, here we don't tell the compiler what currency we want to convert the int to. For that, we will have to do a few things. We will first Import the Class CultureInfo which contains information on a specific culture so we will type CultureInfo then we give it a symbol, then we make a new CultureInfo and between the parentheses we give it the culture we want here we want to get dollars, so between quotes in dash us after if you want in euros you type fr dash FR in uppercase. The c is underlined in red you just need to import the Globalization class because the CultureInfo class belongs to this class. Now we are going to add here and here, the symbol variable. If we launch the program, we have the dollar displayed. So to recap, in this video we saw the formatting of strings with the ToLower(), ToUpper() method, as well as the Trim() method, then we saw the search in a string with the indexof and lastindexof methods. Then the Substring method to create string copies from a given location and a limit. After the Replace method to perform character or substring replacements. We checked whether a string was empty or not with the methods string.IsNullOrEmpty or OrWhiteSpace. Then the split method to split a string and return an array of strings. And to finish converting a string to a number and a number to a string with or without formatting. There is still another method to interact with the strings, I tried to give you the one you will encounter most often, after that you have to look on your side on the Microsoft doc if you need one in particular So here, I hope this video has you more and that everything has been clear for you. See you next time in a new video.