C-SHARP - 4.6 String Advanced
Hello everyone and welcome to this new video. In this video, we are going to deepen a notion that we have seen earlier in this course. We had made a brief introduction on the strings or chains of characters if you want without going into details. Let's start without further ado. A little reminder: the word string is associated with the String class of the DotNet framework. So a string is a class, and you also know that strings are immutable, which means that once you create them, you can't change them. I will now show you some useful methods for working with strings, then I'm going to remind you about conversions from string to number and vice versa with another way of doing it. That's it, so I've categorized these methods into different groups in terms of formatting your string. Here as you can see, we find the ToLower() and ToUpper() methods that we saw in a previous video. The ToLower() method which returns a copy of our string by converting all characters to lowercase. And conversely, the ToUpper() method which returns a copy of our string by converting all characters to uppercase. And as you can see, there is the Trim() method. This method removes spaces at the beginning of a string and at the end. You see here with the example, we have spaces at the beginning and at the end. If we apply the Trim() method, then a will give this result. This method will be very useful to you when you are going to make a website in your future projects. It is possible that the user who registers on your site, will put a space in front of his name. So, if you create an algorithm that sorts customers alphabetically, it will look at the first character of the name. And if it is a space then it can falsify your sort since it is the first character, it was an example but there are many others. Right after we will see a method related to this example. Small precision, the Trim() method does not remove spaces between strings. Then, if you want to search for a character or a word in a string and you want its location, you can use the IndexOf method to get the index of the first letter. Either for example here, we have the chain Hello . If we use the indexof() method and we are looking for the letter n, the program will return its location to us, i.e. 2 since we start at 0, 0 1 2. Here we put a character, but that's it. made possible with a word in a sentence, the method returns the location of the first occurrence of this word, that is to say if a word is present several times in the list, it will return the location of the first one it finds. On the other hand, if it does not find the string given to it as a parameter because I remind you that what is put in the parentheses of a function/method is called a parameter, we will see See you soon in a video. Well, the method will return -1 to say that it is not there. And to finish, there is the LastIndexof method, same principle as the first, this time, as its name indicates, last is last. The method will start to scan the string from the end and at the first occurrence of the string that is passed to it as a parameter then it returns its location. I specify that the index will start from the beginning, since the method starts at the end you never know? case where . If you want to create a substring from a string already given. I explain, basically you have the Hello everyone channel and welcome to this new video. And you want to have a copy of this channel starting with Welcome to this new video. The Hello everyone and you don't want to embed it in this copy. Well, it's the Substring() method that will allow you to do this. In the parentheses of Substring() we will give the index or the location from where? we want to start. Except that we are not going to have fun counting up to the welcome index, here it is still doable but imagine we have a long chain, a paragraph. that would be impossible. Well, we saw a method to do this, I'll let you think about it and we'll see in the next video of the demo how we can manage this. Another thing, this method is a method called overloading. That is to say that there is a method which has the same name as this one in the class except that it has a different number of parameters or a different type of parameter. As you can see, here the Substring() method is overloaded. Indeed, there is another one which comprises 2 parameters. The first, same as the first, we give the index/L location of the o we want to start the copy and the second allows to give a length to this copy because the 1 st copy from the location to the end while the 2nd if we don't want to copy all the rest of the string, just a part so this parameter allows us to do that. I will take a concrete example, you will understand. You see when we receive a notification on our phone. There is only one part of the message if it is too long, only one part is returned so as not to annoy the user, well this parameter uses exactly the same principle. If now, you want to replace a character or a substring/a word in your string then it is possible with the Replace method. This method takes 2 parameters: The element you want to change and the element you want to replace it with. For example, if you want to change all a to e , it is possible. Or if there is a first name for example Jason and you want to change it in a string to Jibril then it is doable with the Replace method Then we have two methods here. String.IsNullOrEmpty() and string.IsNullOrWhiteSpace() which take a chiane as parameter and check if a string is totally empty then it returns true. Otherwise it returns false because it is not empty. The difference between the 2 is that the first must be strictly empty. If there is a space then it returns false because it is not strictly while the 2nd if there is a space, it considers this string as empty and returns true There is a method called split which takes as parameter a separator allowing to split a string. I explain, this method consists in taking a string, we will go through it and each time we will come across the separator that we put in the parentheses is found then we place everything that is before this separator in a array then we continue and we do the same thing until the end of the chain. Once finished, the program returns us an array of strings, each element of this array will be what we place when we reach a separator. For example, the sentence Hello everyone and welcome to this new video. If we apply the split method to this sentence with space as a separator in parameter, we will have this result. We browse the chain, hop a space we put what is before in the table or hello, then we browse again has then a space we put the has in the table so on until the end. Each element of the array will be a word C is a very common technique for summarizing a long string into a smaller one. Now let's move on to the conversion, as we saw in a previous video, it is possible to convert a string into an int if we are in a case where we interact with a user he gives us a number, we will always obtain a string. Even if this string is a number, we have to manually convert it to a number. There are two ways to achieve this: The first is the one we have already seen previously. And there is a second one to convert to Int32. The particularity of the 2nd method is that if the string is empty then it returns the default value for the integer which is zero. Whereas the int.Parse throws an exception. It is therefore easier and safer to work with Convert.ToInt32. And finally, if we have a number and we would like to convert it to a string, we can use the Tostring method on that number. We can call this method without any arguments. Which means you can get a string representation of that number or you can format it using a format string. As you can see here, we gave a parameter to this function, the C is a format string which is short for currency. Basically, here it returns us a currency value in dollars and all three digits are separated by a comma by default when you format a number in currency. It's going to have two decimal numbers. If you don't want a decimal point, you can use C 0 otherwise you leave plain C You can check Microsoft documentation to see the list of common format specifiers in C#, you go to google, type C# numeric format strings and click on the first link. You will find them all. So that's it for this video, in the next video we're going to do a demo of everything we just saw together. In any case, I hope you liked this video and that everything was clear to you. I'll tell you right away.