Now that we know how to define string variables, we need to explore some of the built-in functionality the String class offers. In this video, learn some common methods you can use to manipulate your strings.
- [Instructor] We know how to use the count and is empty methods from the string class. But there are a few others you'll be using on a regular basis. Now these include contains, append, insert, remove, and split, among others. Now here in our test variable comment, let's create a variable called welcome text. And we'll give it initial string called swift four. I'm going to make myself some room under the more string class methods comment.
And just for clarity, let's print out our welcome text so that we can see all the changes we're going to make to it in the debug console. So print welcome text. Perfect. First, let's check if welcome text contains a certain character, so welcome text.contains and here we're going to pass in an element or a character. We're going to use a capital S. Now the contains method is going to pass back a true or false value based on if it finds the given character. So yes, there is a capital S in swift four.
And just so you know, this is case sensitive, so if we pass in a lowercase S, this is going to be false. Next there's the append method. Now we've already looked at how to use the plus operator to concatenate a string onto another string. And this is just a more explicit way of doing that. Welcome text append. And here the Xcode editor is giving us a couple choices. So we can append a character, string, or contents of a sequence, and here we're going to use append other.
And for the string, we're just going to say comma, the adventure continues. Exclamation mark. Now in the debug console you can see our whole string is swift four comma the adventure continues. Now say we wanted to insert a character or an entire string at a specific point into welcome text. We can do that with welcometext.insertmethod. Now again, we have some options here.
We're going to use insert contents of and we're going to use the second option, and here you can see that the definition of this is to insert the elements of a sequence into the collection at a specified position, and that's exactly what we want. If you ever get confused at what your options really are or what they mean, you can go to the documentation and it'll spell it out really clearly for you. So here for our contents of, we're going to say welcome to space, and then we need to give it an index. So we're not going to talk about string indexes in depth here.
But it's enough to know that strings and collection types like arrays start at zero and increment up, and this is called being zero indexed. So the character in welcome text at index zero is a capital S and index one is W and so on. So instead of giving it an integer which Xcode won't accept at this point, we need to hand it welcometext.startindex. Now in our debug console our string has changed to welcome to swift four, the adventure continues.
We can also remove a character or a range of characters, called the substring, from our welcome text variable with the remove method. Now let's try and remove the exclamation mark at the end of our string. So let's type in welcometext.remove. And here we have two choices again, and we're going to take the first one to return the character at a specified index. Now if we try to use the property on welcome text like we did above, well we can try and use the end index property.
But we're going to get an error. So the end index property here is really giving us the character after our last character, so it's looking for something past the exclamation mark, which doesn't exist. So again, fatal error. To fix this and actually remove the exclamation mark, we need to use the index before method on welcome text. So I'm going to delete the method we just had. Go back and choose the first remove option. And here we're going to say welcometext.indexbefore.
And in here we're going to pass it our end index. Now we've got this rightly targeting our last character, the exclamation mark, and in the debug console you can see that it's been removed. The last method we'll look at here is split. Now this is going to go through a string and look for any character we specify. It's then going to take the string before that character and the string after it and return them as two separate values in an array or a collection.
So our string here already has a comma and that's a pretty good way to split this string. So welcometext.split and we're going to take the first option here, separate on character. And for the character, we're just going to give it a comma. I'm going to enlarge the right side inspector so we can actually see what we got back. Here we have an array, which you can tell by two brackets, beginning and end. And inside we have welcome to swift four as the first item, and the adventure continues as the second.
With all these methods, we've seen that there are different permutations available from Xcode, and it's going to give you hints as to what it thinks you want. But if you're looking for a specific functionality, don't be afraid to go to the documentation, because the Swift team may have already provided what you're looking for.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs