1
00:00:02,480 --> 00:00:03,640
Hey everybody.

2
00:00:04,100 --> 00:00:08,170
Welcome back to quote Mark pricer slopes dot com.

3
00:00:08,180 --> 00:00:13,190
And today we are going to talk about strings in the swift Threet.

4
00:00:13,640 --> 00:00:20,910
So let's go ahead and open up our X code 8 and we can actually go to file new playground.

5
00:00:20,930 --> 00:00:21,840
That's what I'm doing here.

6
00:00:21,860 --> 00:00:35,440
File a new playground and let's call this string out strung out because it sounds cool.

7
00:00:35,990 --> 00:00:39,560
And let's go ahead and just save this wherever you like.

8
00:00:41,170 --> 00:00:41,680
OK.

9
00:00:43,630 --> 00:00:44,950
So what is a string.

10
00:00:44,950 --> 00:00:56,110
So we're here in the playground and a string is a core fundamental feature or type of data that you

11
00:00:56,110 --> 00:00:57,440
can use in an application.

12
00:00:57,460 --> 00:00:59,620
And you've witnessed them many times.

13
00:00:59,630 --> 00:01:08,200
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

14
00:01:08,230 --> 00:01:10,170
to the apps.

15
00:01:12,370 --> 00:01:14,440
I actually want to go to the store here.

16
00:01:14,550 --> 00:01:16,040
App Store.

17
00:01:16,590 --> 00:01:17,470
OK.

18
00:01:17,980 --> 00:01:21,790
And here's a bunch of apps right.

19
00:01:21,790 --> 00:01:23,280
Just click on a random map.

20
00:01:23,560 --> 00:01:23,790
OK.

21
00:01:23,800 --> 00:01:25,150
You see these captions here.

22
00:01:25,180 --> 00:01:27,310
Happy rocketship T-Rex.

23
00:01:27,310 --> 00:01:28,480
These are all strings.

24
00:01:28,600 --> 00:01:33,080
At some point there is textual data that is being displayed to the user and that is a string and never

25
00:01:33,100 --> 00:01:34,270
used them in every type of that.

26
00:01:34,330 --> 00:01:37,990
So that's what's going on underneath the hood.

27
00:01:37,990 --> 00:01:42,210
And even right here in the playground the first thing you start off with is the string.

28
00:01:42,220 --> 00:01:45,700
It's a variable named s tr of type string.

29
00:01:45,700 --> 00:01:47,180
And how do you know it's a pipe string.

30
00:01:47,350 --> 00:01:54,670
Well this is called Type inference where your variable is inferring what type you're you're it's going

31
00:01:54,670 --> 00:01:57,490
to be by what you put in it meaning it's a string.

32
00:01:57,490 --> 00:02:01,140
I can't say as Tiahrt equals 50.

33
00:02:01,280 --> 00:02:06,460
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

34
00:02:06,610 --> 00:02:18,430
to type String OK so could also be written as string that's explicitly declared type K whereas before

35
00:02:18,430 --> 00:02:18,930
it was inferred.

36
00:02:18,930 --> 00:02:23,980
So that's a string and a string is a series of characters and you encompass the characters in double

37
00:02:23,980 --> 00:02:25,020
quotation.

38
00:02:25,390 --> 00:02:26,270
OK.

39
00:02:26,890 --> 00:02:29,140
And you can bet any type of characters you want in there.

40
00:02:29,170 --> 00:02:33,840
Even Unicode characters emojis and things like that which is pretty cool.

41
00:02:34,030 --> 00:02:37,090
But you wouldn't put images in a string in your code.

42
00:02:37,090 --> 00:02:38,440
It's absolutely pointless.

43
00:02:38,440 --> 00:02:39,290
Don't do that.

44
00:02:39,630 --> 00:02:40,220
OK.

45
00:02:41,020 --> 00:02:42,650
So what can you do with strings.

46
00:02:42,670 --> 00:02:49,450
Well again just think of anything in a program that has strings you can do anything they've ever seen

47
00:02:49,450 --> 00:02:50,190
in a program.

48
00:02:50,350 --> 00:02:54,400
So if you've seen something in a program that has words in it that's what you can use strings with.

49
00:02:54,400 --> 00:02:56,410
So the options are very open.

50
00:02:56,500 --> 00:02:58,910
So let's just do let's just play around just to show you.

51
00:02:59,140 --> 00:03:03,220
So I'm going to create a new variable called first name like you might have this in an address book

52
00:03:03,220 --> 00:03:04,430
or anything else.

53
00:03:04,630 --> 00:03:09,370
So we'll say jack bar last name equals Bauer.

54
00:03:09,570 --> 00:03:09,990
OK.

55
00:03:10,150 --> 00:03:12,360
Jack Bauer he's a cool guy.

56
00:03:12,400 --> 00:03:13,690
Random errors and prompts.

57
00:03:13,690 --> 00:03:17,290
Currently on the X code beta you may not have these errors and problems.

58
00:03:17,440 --> 00:03:20,150
If you're using the live version which is great.

59
00:03:20,200 --> 00:03:26,140
So first name last name and square and say var full name.

60
00:03:26,140 --> 00:03:27,870
So now you've got a first name and last name right.

61
00:03:27,880 --> 00:03:28,920
Jack Bauer.

62
00:03:29,080 --> 00:03:34,090
And we've assigned it with the equals operator or the assignment operator into this variable so first

63
00:03:34,090 --> 00:03:37,080
name is equal to Jack last name is equal to about right.

64
00:03:37,090 --> 00:03:38,410
So how can we create a full name.

65
00:03:38,410 --> 00:03:41,550
Well we could just create another string right saying Jack Bauer.

66
00:03:41,590 --> 00:03:44,390
Now we're copying and pasting code we never want to do that.

67
00:03:44,590 --> 00:03:48,910
So we can actually do what is called string concatenation.

68
00:03:49,160 --> 00:03:55,390
Because because first name and we can use the addition symbol the plus symbol here plus an empty string.

69
00:03:55,390 --> 00:03:57,930
Actually it's not an empty string plus a space.

70
00:03:58,140 --> 00:03:59,680
Plus the last name.

71
00:04:00,280 --> 00:04:06,220
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

72
00:04:06,220 --> 00:04:09,700
the first name here added and a space here.

73
00:04:09,720 --> 00:04:13,810
OK this is actually a character space and we added the last name.

74
00:04:13,810 --> 00:04:19,000
And notice how we didn't use the words themselves we use the variables because a variable is a representation

75
00:04:19,000 --> 00:04:19,410
of data.

76
00:04:19,410 --> 00:04:27,630
It has a memory location in your program on the hardware and it represents value a value of some kind.

77
00:04:27,640 --> 00:04:28,250
OK.

78
00:04:28,560 --> 00:04:30,700
And so we've just created a file name Jack Bauer.

79
00:04:30,700 --> 00:04:32,980
Now this variable has the string in it.

80
00:04:33,010 --> 00:04:34,990
Very cool very very cool.

81
00:04:34,990 --> 00:04:40,870
So in an application like an address book the data might be stored like this but when you actually open

82
00:04:40,870 --> 00:04:46,680
up your address book on iPhone and it says Jack Bauer or the full name it's actually probably grabbing

83
00:04:46,710 --> 00:04:48,100
these two data types.

84
00:04:48,100 --> 00:04:49,240
Are these two variables here.

85
00:04:49,360 --> 00:04:53,890
And it's concatenated in them or in some way and then it's displaying it to the user which is pretty

86
00:04:53,890 --> 00:04:55,300
cool.

87
00:04:55,300 --> 00:04:55,810
Really cool.

88
00:04:55,810 --> 00:05:00,940
And this is called also called string manipulation or manipulating strings here to do something else

89
00:05:00,940 --> 00:05:07,590
which is what you're going to do all the time as a developer so there's our full name and we can do

90
00:05:07,590 --> 00:05:12,030
it another way to say full name to equals.

91
00:05:12,360 --> 00:05:13,850
Well put some quotes there.

92
00:05:13,860 --> 00:05:18,090
This is an empty string at this point and if you see two quotes like this this is an empty string there's

93
00:05:18,090 --> 00:05:20,070
no characters inside of it.

94
00:05:20,120 --> 00:05:23,980
We're going to do a backslash parentheses.

95
00:05:24,060 --> 00:05:26,940
And then another backslash in parentheses.

96
00:05:27,780 --> 00:05:30,640
And what we're going to do is we're going to put the first name in here.

97
00:05:31,110 --> 00:05:32,800
And the last name.

98
00:05:32,940 --> 00:05:39,750
This is called string Interpol action and what it means is that you can pass variables right into your

99
00:05:39,750 --> 00:05:46,320
string so the backslash says hey backslash parentheses say hey whatever comes in between these parentheses

100
00:05:46,320 --> 00:05:46,900
here.

101
00:05:47,190 --> 00:05:50,720
We're actually going to insert a variable of some kind.

102
00:05:50,720 --> 00:05:54,750
So we're going to insert the first name into here and the last name into here and here is the empty

103
00:05:54,750 --> 00:05:55,280
space.

104
00:05:55,290 --> 00:05:57,080
It gives us the exact same result.

105
00:05:57,570 --> 00:06:02,520
And you know we could play around with it we could we could also let's say we had a number here bar

106
00:06:03,090 --> 00:06:08,350
and let's say age equals 45.

107
00:06:08,550 --> 00:06:11,920
You know we could say Jack Bauer is.

108
00:06:12,000 --> 00:06:18,300
And then another one another insertion here and we put it past the edge and there you have it Jack Bauer

109
00:06:18,570 --> 00:06:20,270
is age 45.

110
00:06:20,620 --> 00:06:21,350
OK.

111
00:06:21,570 --> 00:06:22,000
Pretty good.

112
00:06:22,000 --> 00:06:23,630
So you can insert numbers.

113
00:06:23,970 --> 00:06:29,730
You can enter numbers in here you can insert variables and type a variable that's a value type strings

114
00:06:29,730 --> 00:06:32,450
as well floats doubles things like that.

115
00:06:33,000 --> 00:06:38,250
So funding's you can do with strings you can concatenate them you can insert variables into them using

116
00:06:38,250 --> 00:06:39,470
string Interpol.

117
00:06:39,510 --> 00:06:40,170
That's the keyword.

118
00:06:40,170 --> 00:06:46,980
Write that down and string also string is a class and it has other things available to it.

119
00:06:46,980 --> 00:06:54,320
So another way to add things to it is we could say full name dot append will pass in another string

120
00:06:54,720 --> 00:06:57,630
and then we'll say the third.

121
00:06:57,630 --> 00:06:59,260
And so now Jack Bauer the third.

122
00:06:59,380 --> 00:06:59,690
OK.

123
00:06:59,700 --> 00:07:04,290
So you can call this nifty little append function to append something to the end of it.

124
00:07:04,290 --> 00:07:06,670
You have a lot of things available to you with a string.

125
00:07:06,870 --> 00:07:10,640
And again this fall name right here is a variable of type string.

126
00:07:10,650 --> 00:07:16,710
And when you use the dot syntax you can call different functions or properties on that's on that string

127
00:07:16,710 --> 00:07:17,480
itself.

128
00:07:17,800 --> 00:07:21,930
OK which gives you a lot of opportunities to manipulate strings and do other things.

129
00:07:21,930 --> 00:07:26,880
In fact if you just type in full name and the DOT is there all the things that are available to you

130
00:07:27,000 --> 00:07:29,730
that you can do with your string which is which is pretty cool.

131
00:07:29,730 --> 00:07:33,540
And I'm going to go over all of them and I don't use all of them every single day.

132
00:07:33,600 --> 00:07:37,710
I use a few things a lot and a lot of the things occasionally.

133
00:07:37,770 --> 00:07:42,640
So you can append things but there's more functionality available as well too.

134
00:07:42,870 --> 00:07:43,730
You can.

135
00:07:44,130 --> 00:07:45,380
Well here's a cool one.

136
00:07:45,540 --> 00:07:48,820
Save a book title right in your app.

137
00:07:48,930 --> 00:07:53,490
Let's say it's an app where a user can store the favorite book titles and they decided not to capitalize

138
00:07:53,490 --> 00:07:54,610
anything.

139
00:07:55,140 --> 00:07:56,130
And so just look silly.

140
00:07:56,130 --> 00:08:01,320
So you know their favorite book is revenge of the crab cakes.

141
00:08:01,450 --> 00:08:02,110
OK.

142
00:08:02,460 --> 00:08:03,880
And yeah it's kind of sloppy.

143
00:08:03,900 --> 00:08:04,760
All lowercase.

144
00:08:04,800 --> 00:08:05,450
Doesn't look good.

145
00:08:05,580 --> 00:08:14,410
So what we can do is we can say book title equals and then book title capitalized.

146
00:08:14,520 --> 00:08:18,760
So what's going to happen here is it's going to call this first before it does this.

147
00:08:18,870 --> 00:08:23,910
It's going to grab your book title which is currently this and then we're going to call the capitalized

148
00:08:23,910 --> 00:08:27,640
property on it which is going to return a version of this string.

149
00:08:27,690 --> 00:08:33,270
It's going to return a copy of this except we're the first letter of each word is capitalized and then

150
00:08:33,270 --> 00:08:35,110
it's going to shove it back in the book title.

151
00:08:35,130 --> 00:08:40,650
So book title at this point when it gets here will no longer have a lower case string in it.

152
00:08:40,830 --> 00:08:41,870
OK it will look like this.

153
00:08:41,910 --> 00:08:45,720
So this is another way you might you might have seen this in a program where you put in text and then

154
00:08:45,960 --> 00:08:49,380
the program automatically capitalizes things through to make it look nice.

155
00:08:49,440 --> 00:08:54,530
It's another thing that you have available to you with strings.

156
00:08:54,550 --> 00:09:01,740
What else I have ever been in a chat room where you know everyone cool and chill just typing nicely

157
00:09:01,740 --> 00:09:10,110
and then some punk kids like you know typing in all caps and annoying everybody you know like our chat

158
00:09:10,110 --> 00:09:13,560
room annoying caps guy.

159
00:09:13,640 --> 00:09:14,560
OK.

160
00:09:14,700 --> 00:09:16,620
And he's like Ha.

161
00:09:17,280 --> 00:09:19,040
That is so funny.

162
00:09:19,080 --> 00:09:20,130
Or even worse.

163
00:09:20,160 --> 00:09:23,880
He's like please help me.

164
00:09:24,020 --> 00:09:24,760
Don't do this.

165
00:09:24,880 --> 00:09:30,630
If you're if you're learning how to code don't go into a chat room and say please help me.

166
00:09:30,630 --> 00:09:37,140
Now here is my 100 lines of code.

167
00:09:37,290 --> 00:09:41,160
Don't do this if you've done this before.

168
00:09:41,170 --> 00:09:42,600
Know who you are.

169
00:09:42,730 --> 00:09:46,980
No one no one want to turn on caps is specially Holy smokes.

170
00:09:47,200 --> 00:09:48,860
That's not the way to get people help.

171
00:09:48,900 --> 00:09:51,870
But anyway so yeah annoying chat room guy please help me now.

172
00:09:51,870 --> 00:09:52,860
Here's one hundred lines of code.

173
00:09:52,860 --> 00:09:59,390
Well let's fix that problem so in our app in our chat app we don't want to allow this.

174
00:09:59,400 --> 00:10:03,180
We don't ever want to allow caps so we're going to just force the user never to type in caps so what

175
00:10:03,180 --> 00:10:14,370
we're going to do save our lower case chat equals chat room annoying caps guy thought lower case and

176
00:10:14,370 --> 00:10:19,950
it's annoying caps guy because the females the women they never do it's always cool and chill and nice.

177
00:10:20,070 --> 00:10:25,390
So the guys are like me now and are now ok.

178
00:10:25,530 --> 00:10:27,710
Be nice be nice.

179
00:10:27,720 --> 00:10:30,650
Lowercase chat chat room annoying caps lowercase and now look at it.

180
00:10:30,780 --> 00:10:35,070
It's all lower case that your chat now prevents this from happening so this is a real functionality

181
00:10:35,070 --> 00:10:38,670
you might see in the real world which is kind of cool.

182
00:10:38,850 --> 00:10:41,670
You know these are just different features of the strings that I'm showing you.

183
00:10:41,700 --> 00:10:43,650
You may not even use these all the time.

184
00:10:43,650 --> 00:10:46,780
The ones that you use the most are the ones right here.

185
00:10:46,780 --> 00:10:50,300
That's what is the most when you're working with strings.

186
00:10:50,400 --> 00:10:52,840
What else.

187
00:10:53,190 --> 00:10:59,640
Like what if you like had an app for instance that kids kids could have and you don't want swear words

188
00:10:59,640 --> 00:11:00,320
or something in it.

189
00:11:00,330 --> 00:11:01,800
You know how could you get rid of those things.

190
00:11:01,800 --> 00:11:08,080
Well I'm not going to type swear words into into this program for a million people to see.

191
00:11:08,310 --> 00:11:13,230
But you know I don't know if you guys know this but if you've been around Mormons before they have their

192
00:11:13,230 --> 00:11:15,560
own vocabulary of swear words OK.

193
00:11:15,660 --> 00:11:19,760
Like you know heck they say what do they say.

194
00:11:19,850 --> 00:11:23,780
Oh heck you know that.

195
00:11:24,790 --> 00:11:30,200
You know she is.

196
00:11:31,050 --> 00:11:32,820
But anyway those are some common ones right.

197
00:11:32,820 --> 00:11:33,850
Heck and fetch.

198
00:11:34,030 --> 00:11:39,270
OK so let's say we want an app that gets you know that stops where we're Subha Mormons where it works

199
00:11:39,270 --> 00:11:39,740
OK.

200
00:11:39,930 --> 00:11:43,440
So what we're going to do and I can I can bring this up because I'm a Mormon.

201
00:11:43,440 --> 00:11:47,110
I can say whatever I want so don't think I'm out there offending people.

202
00:11:47,160 --> 00:11:54,120
So I say in this chat up again we want to take out cusswords or in this case the Mormon cusswords you

203
00:11:54,110 --> 00:12:01,170
know we're going to say what the French and heck that is crazy like you know we don't want we don't

204
00:12:01,170 --> 00:12:06,230
want this in our chat room so we can do a little bit of logic which we haven't really covered yet.

205
00:12:06,240 --> 00:12:16,380
But using an if statement and I can say if sentence contains and we can say so if the sentence contains

206
00:12:16,380 --> 00:12:26,960
this word or the double pipes and the symbol under the backspace key or sentence that contains Pech.

207
00:12:27,060 --> 00:12:32,450
All right so if this sentence contains fetch or heck then what can we do.

208
00:12:32,610 --> 00:12:37,810
Well we can say sentence that replacing occurrences of.

209
00:12:37,850 --> 00:12:48,720
So let's replace all occurrences of fetch with tuna and sentence replacing all occurrences of him will

210
00:12:48,720 --> 00:12:53,890
replace heck with player.

211
00:12:54,030 --> 00:12:58,940
Just for fun and like that what the tuna heck.

212
00:12:58,980 --> 00:12:59,550
That is crazy.

213
00:12:59,550 --> 00:13:04,060
And then in this in this case here you know what that player that is crazy.

214
00:13:04,470 --> 00:13:06,060
So pretty cool.

215
00:13:06,060 --> 00:13:12,620
You can do fun little things like that you can you can find occurrence of strange you can remove them.

216
00:13:13,110 --> 00:13:19,830
We covered really like ninety nine point nine percent of what you'll be using strings for over and over

217
00:13:19,830 --> 00:13:24,630
and over and over again which is which is pretty awesome you don't have to know that much in order to

218
00:13:24,630 --> 00:13:27,090
be fully functional and up and running.

219
00:13:27,420 --> 00:13:33,590
So just to repeat some of the key words here and also to recap on variables when you have the colon

220
00:13:33,620 --> 00:13:35,350
and the type here this is.

221
00:13:35,400 --> 00:13:40,440
This means you're explicitly declaring a type or the key word is explicitly declared type if you want

222
00:13:40,440 --> 00:13:41,540
to write that down.

223
00:13:41,610 --> 00:13:48,300
If you take off the colon and the type this is called type inference the variable is inferring that

224
00:13:48,300 --> 00:13:52,380
this is of type string because that's the data that we're passing into it and by the way this is the

225
00:13:52,380 --> 00:13:58,740
recommended way and swift is to use type inference whenever you can and not explicit types can.

226
00:13:59,250 --> 00:14:03,010
You can do string concatenation using the plus symbol.

227
00:14:03,030 --> 00:14:08,880
Adding strings together you can use string interpolation where you can insert variables into strings

228
00:14:09,030 --> 00:14:10,980
like so using the backslash.

229
00:14:10,980 --> 00:14:13,590
And then the parentheses in the variable and.

230
00:14:13,860 --> 00:14:16,560
You can add things to a string using append.

231
00:14:16,650 --> 00:14:21,970
You can do some cool things like capitalize the first letter in each word using the capitalized property.

232
00:14:22,200 --> 00:14:23,710
Capitalize.

233
00:14:23,850 --> 00:14:28,700
You can get lowercase the lower case versions of a word.

234
00:14:28,920 --> 00:14:33,960
And if you were wondering why does this lowercase here have parentheses but capitalized is not that

235
00:14:33,960 --> 00:14:36,830
is it because capitalized is a property.

236
00:14:37,110 --> 00:14:38,040
It's a variable.

237
00:14:38,170 --> 00:14:41,250
OK whereas lowercase is actually a function.

238
00:14:41,250 --> 00:14:44,830
So if I command click this it's not working.

239
00:14:44,830 --> 00:14:51,210
So dominating the show you but this is a property meaning it's a variable in the string class whereas

240
00:14:51,210 --> 00:14:55,390
this is a function in the string class that should do something and return a value.

241
00:14:55,430 --> 00:14:56,510
There is there is a difference.

242
00:14:56,520 --> 00:14:57,770
You'll learn as you go along.

243
00:14:57,930 --> 00:15:03,550
So you get the lowercase version you can get rid of cusswords in an app or whatever you want or just

244
00:15:03,850 --> 00:15:08,140
find strings by using the contains key word and passing in the word and then you can do things with

245
00:15:08,140 --> 00:15:09,770
it like replace.

246
00:15:10,060 --> 00:15:10,960
Really cool stuff.

247
00:15:10,960 --> 00:15:12,430
So this is strings.

248
00:15:12,600 --> 00:15:14,730
You use them all the time not much more to say about it.

249
00:15:14,860 --> 00:15:18,370
Mark Harris devs Lopes dot com and.
