C-SHARP - 3.7 String (Demo)
We now find ourselves on Visual Studio in order to put into practice what we have just seen together. First, we have seen that creating a chain is very simple. We must create a variable of the string type that we will call word for example, and to assign it a string we put in quotes the sentence we want, for example Hello. Then I'm going to create a second string type variable that I'm going to call firstname and I'm going to assign it as a value for example “Medhi” So here we have two strings that are stored in string type variables. And if now we want to create a variable that will store us two strings to obtain the sentence Hello Medhi we will create a third variable of type string that we will call sentence for example and we will assign our 2 variables to it. To do this, you do = we retrieve the word variable then we put a +, the + here will allow us to concatenate our variables, that is to say stick our 2 chains together. Here I will put quotes with a space inside because otherwise Hello and Medhi will be directly glued together without space. The program him, will not directly put a space so we anticipate and put a space. Then I put a + to concatenate with the firstname variable. So now we have a variable that contains our sentence and to display it we do a ConsoleWriteLine() and in parentheses we put the sentence variable. We click on the top to launch the program and you can see that it worked well. Now as I told you in a previous video, this display method is not the best because we are not immune to error in the output window And in C#, we saw that there is a method that allows us to avoid display errors in the output window and to be able to view before launching the program what the program will display. So, we can remove all that, and this method is Format which belongs to the String class. To do this you type string.Format() then between the parentheses you remember that this method takes two parameters. The first is a format string that will allow us to build our string and the second is the variable(s) whose value we want to retrieve to introduce them into our format string. So here we are going to do what we are going to put quotation marks, then we put braces and between its braces we are going to put a number. This number will correspond to the variable that we will put after the comma. Here we will put 0, it uses the same principle as the indexes of an array. The zero will match the first variable after the comma, the 1 to the second and so on Now we will put a comma after the quotation marks and we will recover the variable word And if we want to display the content of the variable sentence, we put in between the parentheses of the ConsoleWriteLine() the variable sentence . We launch the program and you see we have a good hello which is displayed. On the other hand, the sentence we wanted to display was Hello Medhi, so to do this you just have to add braces with the number 1 and add a variable just after it. And if we run the program we can see that we have Bonjour Medhi which is displayed. In addition, we can also add text between our quotation marks just after the brace I can put a comma and I write my name to obtain the following sentence: "Hello, my name is Mehdi." And if I launch the program, the sentence Hello, my name is Mehdi is displayed correctly. And finally, we saw in the previous video that there are special characters when using strings. There is the \n to return to the line, the \t to make a tab Double backslash \\ to introduce a \ in a string for example for a file path etc the list is longer, but we are not going to detail them all the principle is the same each time. To do this we will delete all that, and we will create a chain by typing string that we will call chain and we will assign it any sentence. For example ''Hello my name is toto and I'm 20 years old'' then I do a Console.WriteLine() and between the parentheses I put the variable string. If we launch the program we can see that it is displays on a single line now I would like to display this sentence on two lines starting from "and I am 20 years old" To do this, you simply have to put a \n just before the and, you see that we has a small yellow color for this special character the IDE recognized that it was If now, we launch the program. You see that we have our sentence which is displayed on two lines. Now we are going to create a variable of type string which will contain a file path for example C:\computer \desktop\project\, And well the result will not be the one that we want because when we are going to run the program. the compiler will believe that these backslaches are special characters And we won't get what we want to do If I run the program, you can see that it does something rather odd. So here to remedy this, you have to put a second backslach in front of the backslach so that it understands that here we want to display a backslash in the output window. And if we run the program, we now see that we get the expected result However, you see that this way of doing things is not clean, it's pretty unreadable. And we saw in the previous video, a way to remedy this. This is to add an @ before the quotes. And we can now remove every duplicate back slach, and you see it's much easier to read and understand the program. So each time you want to use a backslash, that you want to make a new line or a tabulation, prefix your string with an at sign to have a correctly formatted string and you will avoid putting these special characters everywhere in your string .