C-SHARP - 3.6 String
Hello everyone and welcome to this new video. In this video we are going to have a brief introduction to channels. And later in the course, we will have a dedicated and more detailed section on chains. First, what is a chain? A string is a sequence of characters and in C# as you can see in this example, a string is surrounded by quotes. So, just like for declaring a variable, we must inform the type is string. Then we give it a name, for example first name, and we assign it a value. For example "Medhi" I will create a second variable of type string that I will call name and I will give it the string "Oubelaid" And to finish I will create a 3rd variable of type string called names which will contain my two variables first name and name. You can also concatenate strings using the + operator. So in this example, I append the first name to an empty string to create a space. And finally the last name. Sometimes this string concatenation method can create weird things when compiling the program. You may have different variables that you are concatenating and it is difficult to visualize the output that one will get If this is the case you can use the Format method In C# this method belongs to the String class of the Dot Net Framework you know that classes can have attributes and methods and we can declare those members to be static, which means we can access them directly in the class without needing to create an object. So in the example you see here, this method of formatting the class string is what is called a static method. So we can access it directly using the string.Format method that takes a few parameters. The first is a format string that we use to build our string, i.e. we will place braces in specific places with a number inside that will allow us to retrieve the variables that we will put after this comma. The brace with the zero corresponds to the 1st variable and the 1 to the second. It uses the same principle as the indexes that we have in the notion of array, the zero corresponds to the first variable Thus, as you see in this example, it's much easier to view the output than to concatenate multiple strings together. Sometimes you may have an array of objects and you can create a string by combining all elements of this array using a separator. So in this example we have an array of integers with the values 1 2 and 3. And in the second line we call the Join method of the String class which is a static method and takes 2 arguments as parameters. The first argument is a separator. In this case, I would like to combine the three numbers using a comma. The second argument of this method is the array whose elements we would like to combine. In summary, here I have an array of int and I create a variable of type string which will contain a string from the values of the array using the Join method and each value will be separated by a separator that we put in the Join method. Here we put a comma but you can put a dot, a dash, etc. Now let's take our first name, know that it is possible to access each of its individual characters using an index For example here, we are going to create a char type variable, you know that char contains only one character and we are going to assign it the first letter of this string. We retrieve the variable and the zero between the brackets will correspond to the first letter of this string. For example, it will match the 2nd etc. One thing you should know about strings in C# is that strings are immutable, which means that once you create them, you can't modify them. So back to our example here, we can certainly read the values of the first character of our string, but we cannot change it. So if for example we try to do that, it won't work and it won't compile. There are methods in the String class that allow us to manipulate strings and change their values. But all these methods return a new string so that the original string is not changed and that's why we say immutable. Another thing, we will now see that there are some special characters in C# which sometimes have different meanings. For example, if you want to have a newline in your string, you must use a backslash followed by an n, then when the compiler sees it, it will consider that it must make a line break. On the other hand if you do not add this backslash, the compiler will simply see a letter N Same if you want to make a tab in your string, you use a backslash followed by a t. This brings us to an interesting situation. What if you want to use a backslash in your string for example to quote it in a website or others because when the compiler sees it as if we are using a special character this does not is not the expected result and it could become problematic. So if we want to tell the compiler that it considers this backslash to be a single character, we have to prefix this backslash with a second backslash And this is what we call escape characters in C# But these are special characters. The list of special characters is actually even longer than that. You can consult the Microsoft documentation but the ones you see in this table are the ones you will commonly use in your life as a C# developer. However, there is a small problem. For example, look at this path variable here, we need to tell the compiler each time it's a backslash by putting a second in front of the main. It's not very clean and mistakes can happen very quickly. In C#, there is one thing that allows us to remedy this, and that thing is an at sign. Thanks to this identifier we don't have to use all those double backslashes, we can just prefix our string with that at sign and therefore we can get rid of those double backslashes and not use only one. We have now finished with the strings, let's now go to Visual Studio to put into practice the notions we have just seen together.