Strings in C# Programming Language | Lukas Vyhnalek | Skillshare
Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
3 Lessons (32m)
    • 1. Introduction

      1:07
    • 2. Strings 01

      15:03
    • 3. Strings 02

      15:54

About This Class

Hi, in this class you will learn how to work with strings in C#.

I will show you all the methods that you can call over the string object and I even explain some use cases, when you should use them.

By knowing these methods you can write better C# code and it can save you a lot of time.

Transcripts

1. Introduction: Okay? Yes. So, in the following couple of lectures, you will learn basically how to work with strings in c sharp. So I will show you. Ah, lot off does building functions so basically functions that somebody else created for you and you can fairly easily use them in C sharp. Um so how it works is basically you have a string and you call some function over the string or some method over the string and it will return some result basically, and those functions are kind of very useful in the real world. So, for example, one of them could be a replace function, So this one basically takes a character and replace it with another one. So, for example, if you have a string and you want to replace all I know ace with bees or whatever, you can do that fairly simply, and you don't necessarily have to write a function all by yourself. You can just use the built in function, so let's get into it 2. Strings 01: All right. So I got here prepared a new file, and this is just a new C sharp file. And let me just create a new variable which will be off the type string. This will be just called value, and I will type in here whatever we want it very much. Doesn't matter what you type in there. And then I will just printed out. So I will just write console that right line. Um can't see it. There it is. And we will write the value, right? So whatever is sword and there we will bring it out. So let me just run the application and see whether everything works properly. And yet it's it's it works pretty much Ah, as you expected and you run the application using control and f five. Cool. So let's start by converting this string into an upper case. So I will just write Consul doubt right line and inside here I will bring the string to upper case. So there is a built in method that takes no argument, OK? And it will basically turn whatever its start and this value into a uppercase drink. So if I run this thing again. As you can see, the second line, whatever you want, right? And it is in upper case, as you may notice, and another one that we can use is converted into a lower case. Right? So to lower, you just type to lower case once again. No argument. Whatever. Like, what would you like to specify? Right. You just want to convert it into lower case. There's no point in passing anything in there. So if I run this again as again see, there was the whatever you want about this time in lower case, right? As you can see, the first W is in lower case. Okay, so this can be sometimes useful in case you, for example, want to remove all duplicates and new Jones going to care about the case or something like dead. It is very useful to keep in. Keep in mind that we can just simply convert a string or a character into uppercase or into lower case using these two methods. Um, another thing that I want to point out ISS, what do you think will be printed right now? So we'll print on the first line, whatever we want. Okay, then on the second line, it will be an upper case. And my question basically iss well, on the third line would be uppercase again. Or will it be just this thing? So basically, I'm asking better. This thing will basically set the new value off this val variable and off course it it doesn't write. It just takes whatever value is stored in there. Somehow transfer it right. So do some stuff with it and then returns it. But it doesn't such the d variable, right? It doesn't set the new value to D variable. So inside here will be just printed this string, right? We don't change the variable with this line. So if I just run the application so that you believe me, as you can see, the strength is their cool. Another thing that you can do is just use the contains. So imagine a case where you just want to find out whether the string contains some character, okay? Or some word, some particular keyword. Right. So imagine that you built a search engine. Okay, so you are building a new google and you have the title of the website and you want to find out whether the key ward is in the title of the website. Okay. And in order to do that and you could use the contains method So inside here I will once again bring it out on the street on the screen. OK, so I will just print out whatever the yourselves will be. And what we can do is just type in here, contains okay. And inside here we can, for example, past some character. So, for example, does it contain w Okay. And if I run this thing so control and a five, as you can see there is true. So it returned True because this string actually contains w, right? There are several off them, right? There it is. There's another one. And for example, what if I do? X So does the string contain X? Well, it doesn't, so it is force right? And it can be very useful in an application when you want to. If you want to find out whether something is industry, right? So basically, whether some subpart off the string actually contains this thing. And another thing is that I want to show you that, um, this is case sensitive. OK, so if I type in here, for example Uppercase t. Okay. Uh, sorry in this ISS and yet So if I type in here offer case T, what do you think will be the result? Well, as I said, it is case sensitive. So, in other words, the lower case letter is different from the upper case. Leather. Okay. And debt means is there any uppercase t? Well, there's not. And it doesn't matter that there is lower case T because it's case sensitive. Okay, so if I run this again, it is false because there is no upper case t right. If I change it to lower case, it will work so well, it will just return true. Okay, It works. Eat away. Okay, Cool. So if I just remove this thing, okay, Like this, and we can move on. And another thing that I want to show you is that you can do this with strings, right? So back to our example. Imagine that we type into the search Mickey world on the keyword, whatever. Like this. OK? And we want to check whether this title, right? So the spell veterans actually includes or contains this keyword and what do you think will be the result of debt? Well, it will be false. Why is that? Well, let me first run it just to see right? It is false. Okay? And why is it false? Um well, big course. As I said earlier, it is case sensitive. So that means this whatever. It has an uppercase w right at the beginning. So if I run this again, it should be true. And it is true. Okay, so, uh, the case matters. And, um, this is just one what I wanted to point out. Now imagine that we have this whatever with the uppercase w. And, uh, we want this contains to return True. Right? So we want to can change the contains so that so that it is not case sensitive. OK. And ah, I would just give you a hint. You can use the to upper or to lower case. So these methods that I just showed you and you feel free to pause the video and think about this problem and try to change this line so that it returns true, And by change, I don't mean changing this to this. Okay, this should state the strings should stay the same. Okay, So if you didn't pause the video, do it now. Because I'm about to tell you how you can actually do it so you can just convert this, for example, to lower case, right? The problem is that they are in different cases, right? This this is in the first letter is an upper case. Right? And inside here it is in lower case. So if we want, we can just change this to lower case, and then it will work, right? So if I run this again, as we can see, there is true and it kind of makes sense because it's wow. Yeah, because it's ah, it's now in lower case, right? We changed it into lower case, right? And then D computer asks OK and doesn't contain this. Whatever. And adults. Right? So you can think of this line. I know it's maybe too too complicated to understand in the indie beginnings, but you can think of it. That computer just goes from left to right, right? And he looks at whatever is in here. Okay? And he says Okay, so I need to convert this variable into lower case and then he stores it at some kind of hidden variable. You can you can You can think of it as a hidden temporary variable dead that only the computer can see. You don't see the hidden variable. Okay. And then on debt hidden variable, he basically calls this contains, okay. And Dan, it actually works. And another thing that I just want to show you is that you can actually have a strength, right? So some another another variable. And that is usually the use case that you have. And inside here, you can obviously type in here d value to right, so you can refer to that as a variable. Cool. All right, so another thing that I just want I want to show you is there? Sometimes you want to ask, for example. And what index is some character in a string, right. So, for example, where is the first de indie indie indie strength? Okay, so in order to do dad right, you can just use once again a building function. So I will once again printed out, and I will just side value and inside here, I want to ask the index off. Okay? And then I won't devalue. So, for example, inside here I want tea, Right? As I said earlier, and I'm basically asking Hey, computer, what is the index off t in this string? And if I run this, the computer answers free. And is it correct? Well, 012 and free, yet it is correct. And what happens if I put another tea in here? So which one will it return? Well, if I run this again, as you can see, the returns, the 1st 1 And so always if I put another t Ok, OK, I just noticed that there waas another t inside here. So if you already noticed that, you probably already knew about it always returns the first index off debt corrector. So basically, it goes from left to right. And once it reached the character, it just returns the index and don't care about us. Okay, So what if I want the the index off the lot of the last, uh, the index of the last character, right. So basically the last Tea Industry Inc. So I can just type in here last index off and let me just run this thing. And as you can see, it a 60 right? And since I don't want to count to 16 I can use another building method. Right? And this can be just Well, it is not method. It is just an attribute of it. And so that means there are no parentheses. And as you can see, the length of the string is 17. Right? So it have 17 characters, and the last character is at index 16. So, yes, string is basically a array off characters, right? Cool. Um, yes. So that's just the index off last inec self Andy length, often array a nothing that you can do is actually replace some characters. So once again, I will just do the right line. And inside here I will just type replace right, this one and I can replace, um, the old character with a new character. Okay. And the second option is that I can replace string with a strength. Okay, So let me start with the simply run character with characters, so I will just replace the w ah with, um j whatever and close it and let's just run this application. And as you can see, it's turned those W's into J right Yeah, cool. And nothing as I mentioned earlier that we can do is just change some string. So, um, we can just change the we word into some another string, and this could be even. You can use even empty string. So this will delete basically all of those we in the string. OK, so if I put another we at the end and run this, it will just return whatever want right? And that is because it just replaced all of those. We in the string with this thing. And this is an empty string, right? I can side whatever I want in here, and it will replace it with whatever I wrote in there, right? And yeah, So another thing that you may know this is that it doesn't necessarily have to be separated by spaces right inside Here is also w E. And it is also replaced by those four s is right. So yeah, and also the length of does doesn't necessarily have to be the same, right? So it is very powerful to that you can use and for example, imagine that you are building a text editor, right? And you are building the that kind of find and replace function, if you know what I mean. And it basically finds all of those characters Well, all of those words, and replace it with the uber did you want, So you can just use this replace method and you get the whole functionality off that feature on one line so that it's pretty cool. 3. Strings 02: Okay. Another thing that we may want to use sometimes is the remove. So inside here again, just type value, and then remove. Sorry. Remove like this. And as you can see, there are two options. So the 1st 1 is that we just defined a start index, and it essentially returns a string that starts at D Start Index. Right. So, for example, if I type inside here Free and Randy application, as you can see, it just returns the work, like like the the first free characters right off the string. So it basically remove all of those characters from this index on, right? So from this index, until the end, it will be removed, right? And another to another, Another way off calling this function. Right. So this is pretty cool, right? You can just remove the first couple off characters, but let's say that we just want to remove the last free characters instead of those first free. So how would you do it with this? Um, with this remove method, well, you could just reverse the string somehow and then do it bar. Um, you can also use just your move method. Okay, So if I just Always, when you type inside here something like remove these visual studio will actually tell you how you can use those function, right. And you can use the up and down keys to actually move and figure out all of those use cases . So the first use case was that we defined the starting index Basically the starting index from which on and we want to delete The second use case can be that, um we define a new string. And yes, So basically a zit says on the screen, right. It returns a new string in which a specified number off characters in the current instance beginning at a specified position have been deleted. So from some position, I want to the lead some count off characters. So you will understand it world with better on an example. So what will happen right now? Well, from the position zero. So that means from the start I want to delete the first free character. So if I, um, Rendez, as you can see it deleted those first free. And there is just the Yeah, the rest of whatever you want, right? And, um yeah, so it deleted these free characters, right? And this thing was your result. All right, so now let's say that we want to delete, um, the all of those characters, except for the last couple. So let's say last four characters, right? So we should get this as a result. So how do we actually do it? Well, I will just keep it based this line and used the remove method for it. So if you type in here, you remove right? It actually suggests you a lot of information about the method. Right? Basically tells you how it works and what arguments you need to pass through this method. Okay. And as you can see, you can move up and down, right There is two off to right so we can move up and down between different use cases. So the 1st 1 was this one. We already use it right from start Index. We want to delete everything. The second is that we want to start at some index, and then we want to delete the account off characters. Okay, so we can actually use this in order to delete all of those characters. Except for the last four. So we want to start a d beginning, right? Because we want to delete those characters from beginning Sudi index off last minus. I had no four or whatever like the number of characters that we want. So inside here I can just taibe the length Oh, sorry. The length like this minus the number of characters. So let's say four. Okay, so I basically want the last four characters. And if I run this as you can see, there are the last four characters, right? The t, the space and the Wnt. Okay, if we check right as you can see the t the space and w any So this is actually how you can do such a thing. And if I, for example, type in here too, what do you think will happen? So right, by the definition, we should get wh Saudi 1st 2 characters and then from the characters from Index to we want to delete all the way. Basically this number of characters, right? So that means we will get in here So we will get w age and then w e So if I run this right , Daryn isn ever you age. W e Okay, so this is very useful. Very useful tools that that that you can actually use and not a very useful to is the sub string. So if I just stop in your council, that bright line and I will show you what the sub string thus so inside here I will just have the sub string right. And there are two possible ways again called us. So the 1st 1 just eggs the start index, and it basically returns a sub string that starts at that specified, like, character position and continue stores the end. So basically the exact opposite off this removed, right? So if I type in here dis Okay, so with free, right, we should get basically the rest except the first free characters, right? As you can see, we got dead. So basically, from this position, all the way to the end is the result. Okay. And inside here is the difference. Debt from zero to this position is the result. Okay. I hope that makes sense. So another way. This is fairly simple use case, right? But you definitely want to use it. Sometimes. You definitely want to. For example, I don't know. You have a you are l that starts if www dot and you want to remove the www dot You can simply just type sub string off four and it will basically remove it in. And yeah, so that's just one. You get you one use case. I just thought off. I'm sure there is a lot of other use cases. Okay, so another way you can actually call this function is dead with with start index and length , and it basically returns a sub string and that starts a debt start index and it have some specified length. So, in other words, it takes the number of characters that you passing there from that start index and returns it so inside here. If I type for example five right, the difference would be that this one will take only the next five characters. Okay? And this one will will take all the way to the end. Okay, So if I run this as you can see, it is all the way to the end. And inside here there are only next five characters. So that is actually how you can use the sub string. Right. And another thing that I want to show you is that you can basically solve this problem, right? So returned the last I know four characters simply using these sub string method. Right? So feel free to post the video and think about it and then solve it. I will just solve it right here. So whatever we want to do, well, we wanted to you our that length minus the number of characters. So let's say the last free characters. So you want to start at the end of the string and take the U last free characters, right? So if I just run this as you can see when you have the space w and E Sony last free characters so you can use these sub string or D remove function. Do you remove function as no, probably more useful if you want some sort of working from the beginning. Rights of some sub string that starts at the beginning on the substrate may be used in different cases, but I mean both of them are pretty much the same. You can basically change change them so that they do the same thing. Okay, cool. Another thing that I want to show you is even easier way to actually check whether a string start with something. Right. So inside here I can just once again type Consul that right line and then I want to check whether it starts with something right. So I will just type start with and then I will pass the string. Right? So basically it doesn't start with I know W h a Okay. And if I put a some corn in there and run this as you can see there is true, right? It is true. It starts with W h A. If I turn this into a lower case and run this again, as you can see, it is false because as I said earlier, it is case sensitive. OK, so uppercase w and lower case w are different characters. Okay, so keep that in mind when he used those functions. And another thing that I want to show you is the ends with So basically the ends with does exactly what would you expect, right? It looks at the end of the string. And if it is same as whatever strongly passing here, it verges return true. If it is not the same, it will return falls What happens if I called us with empty string. Well, he had returns. True. Why is that? Well, is empty string anywhere? Like like, really empty string Basically means that you are asking is a how do I put this? You are asking whether is nothing at the end of the string. And he always can put nothing inside here If that kind of makes sense, this is a weird case. I will just use you a normal, normal use case that you probably want to use in your applications. So that, for example, the we And if I run this, it's is true, right? Because it ends with W and an e cool. Another thing that I want to show you is that you can actually divide a string into a ray off sub string. Right? So imagine that you have some sort off girl, okay? And it basically says, W w whatever you google that come slash something something. Something. Okay. Ah, it's probably looks a bit different bod for some reason you want to split it. Why are these slashes, right? You want to split the character into parts, right? So how do you do it? Well, you can just define a new very about. It will be a ray, and this will be a for example, parts called, and I will just split it right with this separator. Okay, so what this will do is basically split this string right into a array off sub strings, and this will be at the beginning. So this will be at in the zero this or be at index one. This will be at index to this will be at index free four and five. Okay. So if I, for example, printed out So consul, doubt right line, and I will just use the bart at index zero. Okay, so www google that comes should be printed. So if I run this as you can see Derrick de Google that come pretty cool, right? You can, um, often use it, for example, that you have if you have a text editor and you want to somehow get a array off words that are in it You, for example, want to you can split by a space, right, so like this, and it will basically split it by some spaces. So, for example, we will have now www dot g o at in the zero. So if I run this right, as you can see, there is this eso you can split by any character that you want it It's spray cool to use. And I definitely recommend a lot for you to use it in your application. Because if you would create a for example function that would do such a function on the it would be much harder. Advantages used this pre defined function, right? So yeah, Another thing. Last thing that I want to show you is how you can actually remove a spaces. So sometimes you get a string that have a lot of space is in the beginning, and a lot of space is at the end. And, um, I just want to show you how you can remove it very easily because if you would create a function for it, then in the beginning it would be fairly easy to remove it, right, But at the end, not so easy, right? Do you need to check whether you are at the end or not? Right, then you need to remove it. I mean, it's still family easy tax to do. If you our programming for a while, bod. It is still much easier to just use some pre defined function, right or method. So let me, actually first of all, show you that the U. R L is not changed by displayed. Right. So this split doesn't change this variable. This variable will stay the same. Okay. And then I will just remove those spaces. And in order to do that, it is very easy. You just sapping here trim like this. And this will trim out all of these spaces in front of these drink and all of these spaces at the end. And if I run this thing as you can see, Derrida sprinted with those spaces right inside. Here are those spaces. And then on the next line sprinted without them. And so that's pretty much it. Actually, I I hope you enjoyed it. And I hope you learned a lot. And I'll see you next time.