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