Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.2 String

Let's begin our dive into the standard library for Swift by taking a look at the string. Now, we've seen a couple of instances in the previous lessons as examples for creating new strings. So, let's start from the beginning. Now, typically if you're familiar with other programming languages, you are familiar with the concept of initializers or constructors. So, what you might be tempted to do first off is to say, all right, I wanna create a new variable, call this hello. And, we're gonna set this to a new instance of a string. Now, this seems like a reasonable request, but right away you're gonna see that it's not going to work. You're gonna get this little red exclamation point over there and it's pointing out the fact that there's something wrong with new. And, the reason for that is because the new keyword is not needed in creating these new instances or in it, or calling these new initializers within the Swift programming language. So, if we dump the new keyword, now, you're gonna see, we just created an empty string by using this string initializer. Now, there's a couple of different versions of the string initializer, and you're gonna find this for a lot of other types, not only within the Swift programming language but in other places within iOS. But, this is a common way to create new instances of types. So, there's a couple of different, overloaded concepts of a string that we can use. So, now, I can say, all right, now, let's say I wanted to say that hello is equal to a new string, but this time I want to initialize this to something. And, I want to initialize it to "Hello there". So, now, you can see I've created another instance, and we have, or we have overwritten this hello instance of the string. We've just given it another value. So, now, you can see at this point, hello is equal to "Hello there". Now, in previous examples, this is very similar to just using straight initialization, similar to saying var say, hi equal to hi. So, this is the same concept as using this string initializer, just kind of in a shorthand form. Now, you're not very often going to see this more than likely. You're probably going to see initialization and the creation of a new string done this way, shorthand, but just to show you some of the options. Now, a place where this initialization does come in handy is if you're using specialized overloads of that initialization or that constructor method. And, in one instance of that when it comes to a string is to say hello is going to be equal to a string and there's like a couple of different versions down here where you can initialize it to a repeated value given these two examples here. Now, they're slightly different and you may think, oh this isn't so bad, I've seen things like this before. So, what this is going to do is it's going to initialize this instance of a string by concatenating a certain repeated character for a certain number of times that we've specified in here. Now, you're going to see that in Swift, we're able to use the names of the parameters to methods in a similar way that we used the names to those methods in Objective C. So, as you can see here, count, I can keep that parameter name there. And, I can specify an integer value so we can say 5 and then a character. I can put in here "a". Now, you might think this is gonna be just fine, but in actuality it's not gonna work, and it's gonna be a little confusing by looking at these errors, but it's saying there's an extra argument count. So, why is that? Well, actually, we've compu, confused the compiler just a little bit here, because if you recall, when I started to come in here and I said all right this is going to have a repeated value of something, what was left in here was actually a character, not a string. Now, there is a slight difference, obviously, semantically, when it comes to characters and strings where a string is a collection of characters, but, in a lot of other programming languages, what you might be used to is a shorthand notation for creating strings and characters. You might be used to doing something like var character equal to, and maybe using single quotes or something like that. Well, that concept is foreign to Swift. But, what I can do is I can create a character equal to the string of A, but what I'm going to have to do is I'm going to have to stop using this concept of implicit typing; and I'm going to have to explicitly tell Swift and the compiler what the type of character is. And, the way that I do that is by specifying a colon and then giving the type after that. So, I'm going to say that this is actually a character, even though it looks like a string because strings and characters are both represented by those double quotes. So, now, I actually have an instance of a character here and not just a string, even though, it looks very similar here. So, it's one thing you're gonna have to watch out for in the Swift programming language. So, now I can come back and say, hello is going to be equal to and I wanna use that initializer. Where I could say that count, I wanna say 5 and I want the repeated value to be my character, like so. And, now, you're going to get a new instance of a string that has 5 A's. And, that might be something that you would be used to seeing for something like this. So, that's pretty nice. We're able to create new instances of a string now. But, what about some operations I can do on them? What if I need to check the, check to see if they contain some value or if they're empty, or, or things of that nature. So, those are quite simple things to do. There's a couple of different methods and or properties that are on the string type that I can start to draw into use now. I can say, var empty equal to and I can say hello.isEmpty. Now, isEmpty is actually a property and not a method, so, I don't have to put any open closed parentheses. And, you're gonna see here that we check to see if the hello string is empty and stored it into empty and sure enough, we know that it contains at the current point 5 "a", so it truly is not empty. So, another interesting way that you can do this is actually by specifying these properties or methods directly on a literal. So, I can say, my name is Derek and I can say .isEmpty, so it knows that that is an actual string and I can put that property, or that method, or whatever have you on to that actual literal. So, then, you can do so, a couple different interesting thing there, things there. You can give the empty string, obviously, and ask if that's empty, which is truly is. And, then, I could give maybe a couple of white spaces in there, and I can say, see if that's empty. And, you'll see that that is actually false as well. So, white space does not count as empty, but only the actual empty string is going to count as empty. So, now, what if I wanted to see if a particular value is stored within a string? So, let's say that we're gonna specify var greeting is going to be equal to my little greeting here. So, we'll copy this. We'll assign that to the greeting. And, now, I wanna see if that particular string, that greeting, begins with something, or maybe ends with something. So, I can say, greeting.hasPrefix or hasSuffix, so, I can specify a string in the beginning. So, does this have a pris, prefix of "My"? And, you can see that that is actually false and that this is actually case sensitive, cuz as you can see, this has a prefix of "My", where the M is upper case and the y is lower case. So, we'll try this again, hasPrefix, and we'll make sure that we specify it correctly this time, and sure enough, it does begin. So, do remember that it is case sensitive. So, now, we can try the pre, or the suffix, hasSuffix, and we can see if it ends with Derek. Sure enough, it does. Or, we can say, greeting.hasSuffix, doesn't end in "Bob", and no, it does not. So, those are some nice little helper methods that, or properties that you can play around with when you are working with a string type. Now, one other interesting thing that you could probably do and that you will probably see in some data processing style applications is what if I'm given a string value but the value within that string is actually an integer and I need to get it at that value? So, I could say something like var num is going to be equal to "5", so, now how do I coax this num value that's actually a string at this point to be an actual number. Well, I can use the num.toInt method, and this is going to return sum 5. This is actually what's know as an optional. So, as you can see here, we're given the value of sum 5, where the actual value here is 5. And, an optional is kind of similar to other languages if you're used to the concept of a nullible type. So, at this point, because we don't actually know if num, well, we can see that it's an integer within here. But, if we don't actually know that that's an integer, when we call this toInt we don't want problems to happen, so, you're given back an optional. So, another example we can say is var notNum equal to "b", and then, if I try to do notNum.toInt, you're going to see that I'm actually given nil. So, it's basically the equivalent of a null here. So, that's kind of a nice little, nice little thing to have. Like I said that's called an optional. Now, the last few things to really talk about are maybe some working with a string when you need to put things together, like concatenation, or, or the, or the equivalents of strings. Let's say, we wanted to put things together. And, you saw an instance of this in previous lessons where I had something like var name is going to be equal to Derek. So, name is now Derek. But, let's say I wanted to add something to that. Well, I could say, name is gonna be equal to name plus "Jensen" like such. And, now, we have a new instance here where we have updated that instance of name. So, we've actually appended on to that Jensen. And, then, of course, there's the shorthand of that which I'm sure you have probably seen before, where we use the plus equals and maybe we'll just slap onto that an exclamation point, and now, we have Derek Jensen. So, those are some basic operations and usages of the string type within the Swift programming language, that are probably fairly common to you if you've done any sort of other programming in other languages. Just one thing or a couple of things to be weary of, that you do not need to use the new keyword. If you're going to use these initializers, it's not necessary. Even though, it looks like it might, if you're coming from other languages that seem similar to this, and that when you're using some of these methods, be sure to remember that they are going to be case-sensitive. So, if I have a greeting where a word starts with "My" where one of them is lower case, and I try to look for "MY" where both are upper case, that's not going to work. So, that's about enough, I think, for the string type. If you want to look more into a string, you can check the string documentation on the Swift Standard Library. And next, we're going to start talking about arrays.

Back to the top