Hey everybody.
Welcome back to quote Mark pricer slopes dot com.
And today we are going to talk about strings in the swift Threet.
So let's go ahead and open up our X code 8 and we can actually go to file new playground.
That's what I'm doing here.
File a new playground and let's call this string out strung out because it sounds cool.
And let's go ahead and just save this wherever you like.
OK.
So what is a string.
So we're here in the playground and a string is a core fundamental feature or type of data that you
can use in an application.
And you've witnessed them many times.
If you've ever used any type of app and to show you we can go ahead and open up iTunes and we can change
to the apps.
I actually want to go to the store here.
App Store.
OK.
And here's a bunch of apps right.
Just click on a random map.
OK.
You see these captions here.
Happy rocketship T-Rex.
These are all strings.
At some point there is textual data that is being displayed to the user and that is a string and never
used them in every type of that.
So that's what's going on underneath the hood.
And even right here in the playground the first thing you start off with is the string.
It's a variable named s tr of type string.
And how do you know it's a pipe string.
Well this is called Type inference where your variable is inferring what type you're you're it's going
to be by what you put in it meaning it's a string.
I can't say as Tiahrt equals 50.
It's going to yell at me if I try and do that because as TR is the string can assign value of type int
to type String OK so could also be written as string that's explicitly declared type K whereas before
it was inferred.
So that's a string and a string is a series of characters and you encompass the characters in double
quotation.
OK.
And you can bet any type of characters you want in there.
Even Unicode characters emojis and things like that which is pretty cool.
But you wouldn't put images in a string in your code.
It's absolutely pointless.
Don't do that.
OK.
So what can you do with strings.
Well again just think of anything in a program that has strings you can do anything they've ever seen
in a program.
So if you've seen something in a program that has words in it that's what you can use strings with.
So the options are very open.
So let's just do let's just play around just to show you.
So I'm going to create a new variable called first name like you might have this in an address book
or anything else.
So we'll say jack bar last name equals Bauer.
OK.
Jack Bauer he's a cool guy.
Random errors and prompts.
Currently on the X code beta you may not have these errors and problems.
If you're using the live version which is great.
So first name last name and square and say var full name.
So now you've got a first name and last name right.
Jack Bauer.
And we've assigned it with the equals operator or the assignment operator into this variable so first
name is equal to Jack last name is equal to about right.
So how can we create a full name.
Well we could just create another string right saying Jack Bauer.
Now we're copying and pasting code we never want to do that.
So we can actually do what is called string concatenation.
Because because first name and we can use the addition symbol the plus symbol here plus an empty string.
Actually it's not an empty string plus a space.
Plus the last name.
So if you look over here in the right hand side it now says Jack Bauer So all we've done is we've taken
the first name here added and a space here.
OK this is actually a character space and we added the last name.
And notice how we didn't use the words themselves we use the variables because a variable is a representation
of data.
It has a memory location in your program on the hardware and it represents value a value of some kind.
OK.
And so we've just created a file name Jack Bauer.
Now this variable has the string in it.
Very cool very very cool.
So in an application like an address book the data might be stored like this but when you actually open
up your address book on iPhone and it says Jack Bauer or the full name it's actually probably grabbing
these two data types.
Are these two variables here.
And it's concatenated in them or in some way and then it's displaying it to the user which is pretty
cool.
Really cool.
And this is called also called string manipulation or manipulating strings here to do something else
which is what you're going to do all the time as a developer so there's our full name and we can do
it another way to say full name to equals.
Well put some quotes there.
This is an empty string at this point and if you see two quotes like this this is an empty string there's
no characters inside of it.
We're going to do a backslash parentheses.
And then another backslash in parentheses.
And what we're going to do is we're going to put the first name in here.
And the last name.
This is called string Interpol action and what it means is that you can pass variables right into your
string so the backslash says hey backslash parentheses say hey whatever comes in between these parentheses
here.
We're actually going to insert a variable of some kind.
So we're going to insert the first name into here and the last name into here and here is the empty
space.
It gives us the exact same result.
And you know we could play around with it we could we could also let's say we had a number here bar
and let's say age equals 45.
You know we could say Jack Bauer is.
And then another one another insertion here and we put it past the edge and there you have it Jack Bauer
is age 45.
OK.
Pretty good.
So you can insert numbers.
You can enter numbers in here you can insert variables and type a variable that's a value type strings
as well floats doubles things like that.
So funding's you can do with strings you can concatenate them you can insert variables into them using
string Interpol.
That's the keyword.
Write that down and string also string is a class and it has other things available to it.
So another way to add things to it is we could say full name dot append will pass in another string
and then we'll say the third.
And so now Jack Bauer the third.
OK.
So you can call this nifty little append function to append something to the end of it.
You have a lot of things available to you with a string.
And again this fall name right here is a variable of type string.
And when you use the dot syntax you can call different functions or properties on that's on that string
itself.
OK which gives you a lot of opportunities to manipulate strings and do other things.
In fact if you just type in full name and the DOT is there all the things that are available to you
that you can do with your string which is which is pretty cool.
And I'm going to go over all of them and I don't use all of them every single day.
I use a few things a lot and a lot of the things occasionally.
So you can append things but there's more functionality available as well too.
You can.
Well here's a cool one.
Save a book title right in your app.
Let's say it's an app where a user can store the favorite book titles and they decided not to capitalize
anything.
And so just look silly.
So you know their favorite book is revenge of the crab cakes.
OK.
And yeah it's kind of sloppy.
All lowercase.
Doesn't look good.
So what we can do is we can say book title equals and then book title capitalized.
So what's going to happen here is it's going to call this first before it does this.
It's going to grab your book title which is currently this and then we're going to call the capitalized
property on it which is going to return a version of this string.
It's going to return a copy of this except we're the first letter of each word is capitalized and then
it's going to shove it back in the book title.
So book title at this point when it gets here will no longer have a lower case string in it.
OK it will look like this.
So this is another way you might you might have seen this in a program where you put in text and then
the program automatically capitalizes things through to make it look nice.
It's another thing that you have available to you with strings.
What else I have ever been in a chat room where you know everyone cool and chill just typing nicely
and then some punk kids like you know typing in all caps and annoying everybody you know like our chat
room annoying caps guy.
OK.
And he's like Ha.
That is so funny.
Or even worse.
He's like please help me.
Don't do this.
If you're if you're learning how to code don't go into a chat room and say please help me.
Now here is my 100 lines of code.
Don't do this if you've done this before.
Know who you are.
No one no one want to turn on caps is specially Holy smokes.
That's not the way to get people help.
But anyway so yeah annoying chat room guy please help me now.
Here's one hundred lines of code.
Well let's fix that problem so in our app in our chat app we don't want to allow this.
We don't ever want to allow caps so we're going to just force the user never to type in caps so what
we're going to do save our lower case chat equals chat room annoying caps guy thought lower case and
it's annoying caps guy because the females the women they never do it's always cool and chill and nice.
So the guys are like me now and are now ok.
Be nice be nice.
Lowercase chat chat room annoying caps lowercase and now look at it.
It's all lower case that your chat now prevents this from happening so this is a real functionality
you might see in the real world which is kind of cool.
You know these are just different features of the strings that I'm showing you.
You may not even use these all the time.
The ones that you use the most are the ones right here.
That's what is the most when you're working with strings.
What else.
Like what if you like had an app for instance that kids kids could have and you don't want swear words
or something in it.
You know how could you get rid of those things.
Well I'm not going to type swear words into into this program for a million people to see.
But you know I don't know if you guys know this but if you've been around Mormons before they have their
own vocabulary of swear words OK.
Like you know heck they say what do they say.
Oh heck you know that.
You know she is.
But anyway those are some common ones right.
Heck and fetch.
OK so let's say we want an app that gets you know that stops where we're Subha Mormons where it works
OK.
So what we're going to do and I can I can bring this up because I'm a Mormon.
I can say whatever I want so don't think I'm out there offending people.
So I say in this chat up again we want to take out cusswords or in this case the Mormon cusswords you
know we're going to say what the French and heck that is crazy like you know we don't want we don't
want this in our chat room so we can do a little bit of logic which we haven't really covered yet.
But using an if statement and I can say if sentence contains and we can say so if the sentence contains
this word or the double pipes and the symbol under the backspace key or sentence that contains Pech.
All right so if this sentence contains fetch or heck then what can we do.
Well we can say sentence that replacing occurrences of.
So let's replace all occurrences of fetch with tuna and sentence replacing all occurrences of him will
replace heck with player.
Just for fun and like that what the tuna heck.
That is crazy.
And then in this in this case here you know what that player that is crazy.
So pretty cool.
You can do fun little things like that you can you can find occurrence of strange you can remove them.
We covered really like ninety nine point nine percent of what you'll be using strings for over and over
and over and over again which is which is pretty awesome you don't have to know that much in order to
be fully functional and up and running.
So just to repeat some of the key words here and also to recap on variables when you have the colon
and the type here this is.
This means you're explicitly declaring a type or the key word is explicitly declared type if you want
to write that down.
If you take off the colon and the type this is called type inference the variable is inferring that
this is of type string because that's the data that we're passing into it and by the way this is the
recommended way and swift is to use type inference whenever you can and not explicit types can.
You can do string concatenation using the plus symbol.
Adding strings together you can use string interpolation where you can insert variables into strings
like so using the backslash.
And then the parentheses in the variable and.
You can add things to a string using append.
You can do some cool things like capitalize the first letter in each word using the capitalized property.
Capitalize.
You can get lowercase the lower case versions of a word.
And if you were wondering why does this lowercase here have parentheses but capitalized is not that
is it because capitalized is a property.
It's a variable.
OK whereas lowercase is actually a function.
So if I command click this it's not working.
So dominating the show you but this is a property meaning it's a variable in the string class whereas
this is a function in the string class that should do something and return a value.
There is there is a difference.
You'll learn as you go along.
So you get the lowercase version you can get rid of cusswords in an app or whatever you want or just
find strings by using the contains key word and passing in the word and then you can do things with
it like replace.
Really cool stuff.
So this is strings.
You use them all the time not much more to say about it.
Mark Harris devs Lopes dot com and.