From the course: Learning C#

String operations - C# Tutorial

From the course: Learning C#

String operations

- [Instructor] At some point, almost all non-trivial applications have to work with string data. And we're going to spend this chapter examining how C# makes it easy to process string content. So we're going to start off by trying some basic string operations, and as we go through the chapter, we'll see how to perform more advanced operations. So here in the Operations folder, in our Strings folder, I'll open the program code and you can see I have some string variables defined already. And just like everything else in C#, strings are objects and they have properties that we can examine to find out some of their information. So, for example, if I wanted to get the length of a string, I can use the length property. So I'll go ahead and print out str1.length. And that's the property on the string. So, in other languages like Python, for example, you might use the len function to get the length of a string, but here, it's a property. We can also access individual characters in the string by using square brackets, as if it were an array. So, for example, if I wanted to get the 14th character of str1, I would write str1, and then inside the square brackets, the number 14. And strings are sequences of characters, so we can iterate over them using loops, just like we can with other sequences like arrays. So, for example, I can use a foreach loop, which we learned about earlier in the course, and I'm going to just declare a character variable. So for each char in str1, I'm going to Console.Write, not WriteLine. I'm going to write the character. And then if the character is equal to the letter b, then I'm going to write a empty line and then break out of the loop. But again, we learned all about that in the loops chapter. Let's save and let's try out what we have so far. So I'm going to bring up my integrated terminal. And I'll just go ahead and run this. So I'll dotnet run. All right, we've got some warnings about unused variables, but they'll be used soon. And we can see in the output that the length of the string is 44 characters and the 14th character in str1 is the letter n. So it's going to be this guy right here. And then you can see that we iterate over the str1 characters until we weeks the letter b, and then we stop. All right, so, so far so good. Let's try a few more operations. I'll close the terminal. So the Concat function can be used to add strings together to make one string. So I'll use my outstr variable, and I've declared that up here, that string is out outstr, and I'm going to set that equal to the results of the String class's Concat function. And I'm going to pass the strs variable, which is this array of strings right here. I'm going to concatenate that all into one string and then I'll just go ahead and print the result. We can also use the Join function. So the Join function creates a single string from multiple strings and you can specify a character or string to separate each one of the joined strings. So, for example, I can set outstr equal to, and I'll call String.Join, and I'll separate each one of the strings with a period, and I'll call that on strs again, and then I'll just go ahead and copy this and print outstr. And then let's do one more example. So I'll copy these and paste. And instead of using one character, I'll use a string with three dashes in it. Okay, so let's go and save. Now let's run our updated code. So let's bring our terminal back. All right. Let's run this. So now we can see that this is the result of the Concat function with the strs being printed out. Then we have the next example where we have all those strs, but was separated by periods, and then finally separated by dashes. So, comparing strings is also pretty easy. So let's go ahead and update the code to do that. The first function is called the Compare function and it's called directly on the String class. It's not an object method. You call it directly on the String class. And it performs what's called an ordinal comparison of two strings. So I'm going to declare a result, and this variable is going to be an integer, and I'm going to call a String.Compare. So the compare function will return an integer value. If that value is less than zero, that means that the first string comes before the second one if you were sorting them. Zero means that they would be at the same position, and then greater than zero means that the first string comes after the second one in a sorting order. So if I were to compare str2 with This is a string, and if we scroll up, we'll see that str2 is actually that string. It's This is a string. So this should return zero because they're pretty, you know, they're the same thing. Now, note that this function is used when you're sorting strings, it's not used to check for equality. If you just want a straight boolean and result, like a true/false result of whether or not one string equals another one, you can use the Equals function for that. So I'll define an isEqual variable and call str2.Equals, and I'll compare that to str3. And then we'll go ahead and Console.WriteLine, and we'll just simply write out isEqual. All right. And you can see up here, let me scroll up, this is str2 right here. str3 is the same letters, but some of them are uppercase. So that should not be true. That's going to return false because they're not the same. Okay, let's go ahead now and run what we have. We've got Compare and Equals. So let's save, let's bring our terminal back up and run again. And now we can see sure enough that the... Oh, whoops. I didn't print out the result. Let me do that. All right. Let's run that again. All right, so the result is zero because the strings are pretty much the same. And then the Equals function returns false because str3 has some uppercase characters in it. So let's finish up by trying some string searching. Let's close this again. So let's take a look at the IndexOf function. So I'm going to use Console.WriteLine. And what I'm going to do is write out str1. I'm going to call the IndexOf function and I'm going to look for the index of the letter e. So this will tell me the first index of the letter e in that string or -1 if it's not there. And I can do the same thing with regular strings, not just characters. So, for example, if I were to look for the index of, say, fox in str1, and again, if we scroll up, you'll see str1 is The quick brown fox. We've been using that string a little bit now. And then, similarly, there's the LastIndexOf function. So let's go ahead and copy these two lines, and paste them in. And now I'm going to call LastIndexOf. And I'll LastIndexOf e and then last index of the. And one more thing we'll do is a search and replace. So we can use the Replace function to replace content in the string. So I'll set outstr equal to str1 and I'll call the Replace function. And I'm going to replace the word fox with the word cat. And then once again we'll go ahead and Console.WriteLine outstr. And then just to prove that it's gone, we're going to write out outstr's IndexOf and we're going to look for fox. That should be -1 after it's gone. All right, so let's run these updated examples Let's bring up our terminal. Okay, so let's go ahead and scroll here. So we've got IndexOf e, so that's going to be index two, if we scroll up and see the string. Kay? So, yep, sure enough, there's e, index two. And then we've got, let's see, 16 for, let's scroll back down, so index of fox is at 16. The last index of e is at 33. The last index of the word the is at 31. So then we do the replace, and sure enough, fox is now replaced by cat, and sure enough, - 1 is the result when we try to look for fox. So the C# String class is very rich in functionality and we've only just scratched the surface here. If you want to check out more of the functions available, you can look at the String class documentation which is available at this link. And you might want to keep this open as we go through the rest of the chapter to refer back to in case you want to see how the methods work or take a look at any of their variants. So keep this open as we go through the rest of the chapter, but you can read through these docs and see for yourself how many different kinds of properties and methods are available on the String class.

Contents