1
00:00:08,370 --> 00:00:11,682
Well, here we are in Module Six, where
we're going to

2
00:00:11,682 --> 00:00:16,140
learn about strings, and here are the
Module Six learning objectives.

3
00:00:20,530 --> 00:00:24,688
We're going to move back away from game of
a, game development for a little while

4
00:00:24,688 --> 00:00:27,271
to cover some more core programming
topics, but

5
00:00:27,271 --> 00:00:30,580
don't worry, we'll get back to it soon.

6
00:00:30,580 --> 00:00:33,153
So in this module, we're going to be

7
00:00:33,153 --> 00:00:37,900
looking at an incredibly useful data type
called string.

8
00:00:37,900 --> 00:00:40,510
And so, the core idea.

9
00:00:40,510 --> 00:00:42,427
Is that strings are sequences of

10
00:00:42,427 --> 00:00:45,835
characters, those unicode characters that
we actually

11
00:00:45,835 --> 00:00:47,880
talked about before.

12
00:00:47,880 --> 00:00:52,500
So let's just jump right in and start
using strings.

13
00:00:52,500 --> 00:00:56,855
I have already built a small project, the
console application, that we're

14
00:00:56,855 --> 00:01:01,570
going to use to explore some of the
fundamental ideas behind strings.

15
00:01:01,570 --> 00:01:05,440
So one of the things that strings are
really useful for

16
00:01:05,440 --> 00:01:10,890
is getting user input, and processing that
user input in some way.

17
00:01:10,890 --> 00:01:14,750
So, that's what, that's what we'll be
doing today is we'll be.

18
00:01:14,750 --> 00:01:18,280
Getting input from the user and processing
it.

19
00:01:18,280 --> 00:01:20,888
So let's actually

20
00:01:20,888 --> 00:01:21,703
[NOISE]

21
00:01:21,703 --> 00:01:23,496
prompt for,

22
00:01:23,496 --> 00:01:24,311
[NOISE]

23
00:01:24,311 --> 00:01:25,778
and read in

24
00:01:25,778 --> 00:01:26,593
[NOISE]

25
00:01:26,593 --> 00:01:28,223
gamer tag,

26
00:01:28,223 --> 00:01:29,038
[NOISE]

27
00:01:29,038 --> 00:01:31,475
print out values

28
00:01:31,475 --> 00:01:36,750
[SOUND].

29
00:01:36,750 --> 00:01:39,550
And that extra line that I always like to
add.

30
00:01:43,810 --> 00:01:48,230
So, to prompt for and read in the
gamertag.

31
00:01:48,230 --> 00:01:49,660
The first thing is the prompt.

32
00:01:49,660 --> 00:01:52,540
We're going to ask the user to input
something.

33
00:01:52,540 --> 00:01:56,650
And the way we'll do that is we'll do
console dot write.

34
00:01:56,650 --> 00:01:59,684
Now, you'll notice that I didn't do write
line

35
00:01:59,684 --> 00:02:03,310
here, because when we prompt users for
input, it's much

36
00:02:03,310 --> 00:02:05,974
nicer for them to be able to enter that
input

37
00:02:05,974 --> 00:02:09,378
right after the prompt instead of on the
line following

38
00:02:09,378 --> 00:02:10,530
the prompt.

39
00:02:10,530 --> 00:02:15,680
So all the prompts you ever do in console
apps should use write.

40
00:02:15,680 --> 00:02:16,510
Not write line.

41
00:02:19,050 --> 00:02:21,040
I'm going to say, enter gamertag.

42
00:02:22,880 --> 00:02:26,440
Now you'll also notice that I haven't been
polite at all.

43
00:02:26,440 --> 00:02:29,842
You'd think I'd say please enter gamertag
because I'm asking

44
00:02:29,842 --> 00:02:33,260
them for a favor, to provide input to my
program.

45
00:02:33,260 --> 00:02:38,474
However, what happens is all your prompts
end up having to start with please if

46
00:02:38,474 --> 00:02:41,081
you go down this road and that's really

47
00:02:41,081 --> 00:02:45,060
actually far more irritating than you
might think.

48
00:02:45,060 --> 00:02:50,120
So, this'll, this'll be your opportunity
to be impolite in your prompts.

49
00:02:50,120 --> 00:02:52,160
Go ahead and just prompt away without any

50
00:02:52,160 --> 00:02:55,560
pleases, or thank yous, or any of that
stuff.

51
00:02:55,560 --> 00:02:55,860
Okay.

52
00:02:55,860 --> 00:02:58,870
So we've done the prompt, we've displayed
this prompt and what the heck?

53
00:02:58,870 --> 00:03:05,210
Let's actually Ctrl+F5 for console apps,
let's Ctrl+F5 to see what happens.

54
00:03:05,210 --> 00:03:10,456
Okay, it prints the prompt But it doesn't
actually give us that blank

55
00:03:10,456 --> 00:03:11,650
line yet.

56
00:03:11,650 --> 00:03:15,095
It's because it left the cursor right
here, and then the write

57
00:03:15,095 --> 00:03:18,350
line moved it here, and then we get the
press any key.

58
00:03:18,350 --> 00:03:22,080
So, we'll get to the point, don't worry,
where we have that blank line.

59
00:03:22,080 --> 00:03:24,050
But it does print out the prompt properly.

60
00:03:25,150 --> 00:03:28,150
So now, we want to read in.

61
00:03:28,150 --> 00:03:31,520
The gamer tag, and the gamer tag is a
sequence of characters.

62
00:03:31,520 --> 00:03:32,710
So we want to take

63
00:03:32,710 --> 00:03:33,135
[NOISE]

64
00:03:33,135 --> 00:03:38,510
this gamer tag, and we want to put it into
a string variable.

65
00:03:38,510 --> 00:03:43,590
Now, you might have noticed, I said, I
didn't say, string is a class.

66
00:03:43,590 --> 00:03:46,446
String is a reference type, it's not a
value type, that's why we

67
00:03:46,446 --> 00:03:50,150
haven't covered it, why we didn't cover it
way back in the value types.

68
00:03:50,150 --> 00:03:54,278
But this format of a lowercase and it
turns it bright

69
00:03:54,278 --> 00:03:58,502
blue for a keyword instead of light blue
for a class,

70
00:03:58,502 --> 00:04:03,398
is because strings are so commonly used
that we get sort of two

71
00:04:03,398 --> 00:04:08,486
ways to think about them.
This and this, string the

72
00:04:08,486 --> 00:04:13,601
class, string the data type, they're the
same exact thing, so

73
00:04:13,601 --> 00:04:18,628
I tend to use lower case string when I'm
using it as a data type.

74
00:04:18,628 --> 00:04:23,527
And upper case string when I'm accessing
properties of the string or method of the

75
00:04:23,527 --> 00:04:27,858
string class but it's either personal
preference or it's, you know,

76
00:04:27,858 --> 00:04:32,720
coding standards you have to follow in
your game company or whatever.

77
00:04:32,720 --> 00:04:35,030
So, string; I have a string.

78
00:04:36,120 --> 00:04:38,640
I will call it gamer tag because it's
going to store the gamer tag.

79
00:04:38,640 --> 00:04:40,780
So, lets use a good variable name for it.

80
00:04:42,490 --> 00:04:46,130
Now, we've seen this use of console
before.

81
00:04:46,130 --> 00:04:48,668
We've used console dot write, we've used
console dot

82
00:04:48,668 --> 00:04:50,020
write line.

83
00:04:50,020 --> 00:04:58,520
Here, we can use console.readline to
actually read in a line from the user.

84
00:04:59,580 --> 00:05:02,573
So it will read in whatever keys they type
up until

85
00:05:02,573 --> 00:05:07,540
they press Enter, and then it returns the
resulting string to us.

86
00:05:07,540 --> 00:05:09,556
So you should go do an in-lecture quiz

87
00:05:09,556 --> 00:05:12,400
to demonstrate to me that you understand
that idea.

88
00:05:16,000 --> 00:05:18,501
So, we've done that, let's, now that we've

89
00:05:18,501 --> 00:05:21,150
read in this value, let's actually print
it out.

90
00:05:22,310 --> 00:05:24,790
So console dot write line.

91
00:05:26,710 --> 00:05:29,520
This isn't a prompt, it's output, so we'll
just do write line.

92
00:05:30,930 --> 00:05:32,906
We'll say that we're printing out the
gamer

93
00:05:32,906 --> 00:05:34,870
tag, I might as well just capitalize that.

94
00:05:37,940 --> 00:05:41,640
And then concatenate the actual gamer tag
to it.

95
00:05:41,640 --> 00:05:44,965
So now when I Ctrl+F5, it asks me for the

96
00:05:44,965 --> 00:05:49,530
gamer tag, here's my gamer tag, it's an
awesome one.

97
00:05:50,820 --> 00:05:55,709
It prints out that gamer tag, and then I
get the press any key to continue.

98
00:05:55,709 --> 00:05:58,901
So we've now used console dot read line to
read in a

99
00:05:58,901 --> 00:06:03,400
string, we haven't done any processing to
it but we've read it in.

100
00:06:03,400 --> 00:06:08,549
And we're going to, and we printed it out.
The next thing will be a little more

101
00:06:08,549 --> 00:06:14,530
interesting.
So, let's prompt for and read in.

102
00:06:14,530 --> 00:06:15,810
Their level.

103
00:06:15,810 --> 00:06:17,945
Whatever that means and whatever genre of

104
00:06:17,945 --> 00:06:20,740
game we're building here as a console
application.

105
00:06:24,120 --> 00:06:26,200
So let's console that right.
Here's the prompt.

106
00:06:29,970 --> 00:06:31,220
And I'll say, enter level.

107
00:06:33,670 --> 00:06:38,959
And now we have a problem because a level
should be an

108
00:06:38,959 --> 00:06:44,304
integer.
There are no level 83.7 paladins

109
00:06:44,304 --> 00:06:49,850
so, or images, depending on your game,
right.

110
00:06:49,850 --> 00:06:52,390
So, we want to put this into an int.

111
00:06:52,390 --> 00:06:58,430
But if I say int level equal console dot
read line.

112
00:07:01,680 --> 00:07:03,830
We have a real problem.

113
00:07:03,830 --> 00:07:07,184
And the real problem that we have is that
we're

114
00:07:07,184 --> 00:07:11,510
getting this compilation error that I will
F6 to show you.

115
00:07:13,600 --> 00:07:16,680
And it says, we can't implicitly convert
from

116
00:07:16,680 --> 00:07:20,270
a string to an integer, which makes sense,
right?

117
00:07:20,270 --> 00:07:21,550
We've talked about data types.

118
00:07:21,550 --> 00:07:24,670
We've talked about type conversions and
type casting.

119
00:07:24,670 --> 00:07:26,290
And there's no way.

120
00:07:26,290 --> 00:07:31,080
That it can, the compiler can figure out
that a string is an integer.

121
00:07:31,080 --> 00:07:32,920
because a string's a bunch of characters.

122
00:07:32,920 --> 00:07:36,730
An integer's a bunch of bits that
represent a number.

123
00:07:36,730 --> 00:07:38,746
So, we need to turn

124
00:07:38,746 --> 00:07:42,460
this string into an integer.

125
00:07:42,460 --> 00:07:44,460
And you might think, based on our
conversation

126
00:07:44,460 --> 00:07:46,530
about type casting, that we could just do
this.

127
00:07:47,800 --> 00:07:48,725
But, we can't.

128
00:07:48,725 --> 00:07:54,410
We cannot do a straight type cast, so
we're going to have to do something else.

129
00:07:54,410 --> 00:07:56,842
And the something else we're going to do

130
00:07:56,842 --> 00:07:57,222
[NOISE]

131
00:07:57,222 --> 00:08:00,642
is we're going to take advantage of the
fact that the int

132
00:08:00,642 --> 00:08:04,746
datatype, in fact, even though we've
called it a value type,

133
00:08:04,746 --> 00:08:07,710
and we use it as a value type, it actually
has,

134
00:08:07,710 --> 00:08:12,570
hidden behind the scenes if you will, a
reference type as well.

135
00:08:12,570 --> 00:08:15,755
And that reference type exposes a method
called parse.

136
00:08:15,755 --> 00:08:16,270
[SOUND]

137
00:08:16,270 --> 00:08:22,141
Now, the term parse comes from compiler
stuff, but basically what

138
00:08:22,141 --> 00:08:29,050
this parse means is, take the string of
characters and turn it into.

139
00:08:29,050 --> 00:08:34,070
It's representation as an integer, and
that's probably good enough for now.

140
00:08:34,070 --> 00:08:38,030
Compilers, total side topic, building a
compiler in a compiler

141
00:08:38,030 --> 00:08:41,342
class is probably one of the most useful
things you'll

142
00:08:41,342 --> 00:08:45,590
end up doing as a programmer, because
there are so many ideas.

143
00:08:45,590 --> 00:08:49,650
That apply throughout computer science and
programming in particular.

144
00:08:49,650 --> 00:08:52,190
But we're not going to build a compiler
today.

145
00:08:52,190 --> 00:08:56,270
We're just going to parse this input
string into an integer

146
00:08:56,270 --> 00:08:59,870
and then we'll print it out.
So,

147
00:08:59,870 --> 00:09:08,868
console.writeLine level.

148
00:09:08,868 --> 00:09:11,860
Like so.

149
00:09:11,860 --> 00:09:16,070
So I'm going to Ctrl+F5, so that's for the
gamer tag, and that's for the level 30.

150
00:09:16,070 --> 00:09:16,570
Newb.

151
00:09:18,040 --> 00:09:20,640
And, there we go, it prints that stuff
out.

152
00:09:20,640 --> 00:09:23,874
So, printing this stuff out, I'm starting
to get irritated that there's no

153
00:09:23,874 --> 00:09:26,980
space between the input and the output, so
I'll add an extra line here.

154
00:09:29,240 --> 00:09:31,700
But I will also point out, that that

155
00:09:31,700 --> 00:09:36,040
int parse thing makes an assumption about
the user.

156
00:09:36,040 --> 00:09:41,460
And the assumption it makes about the user
is that the user is not an idiot.

157
00:09:41,460 --> 00:09:45,590
So, in fact, if I run this, and I enter a
gamer tag.

158
00:09:45,590 --> 00:09:48,850
And it has Level and I say Bob.

159
00:09:50,430 --> 00:09:53,120
Bad news.
Crash.

160
00:09:53,120 --> 00:09:54,480
Horrible thing.

161
00:09:54,480 --> 00:09:56,440
Close the program.
Should we debug it?

162
00:09:56,440 --> 00:09:56,850
No.

163
00:09:56,850 --> 00:09:58,550
Go online?
No.

164
00:09:58,550 --> 00:10:02,110
There is no online solution for the user
being a moron.

165
00:10:02,110 --> 00:10:02,900
I'm sorry.

166
00:10:02,900 --> 00:10:06,720
Unfortunately, searching the web won't
solve this problem, right.

167
00:10:06,720 --> 00:10:10,805
So, a user who does not enter an integer
when we're

168
00:10:10,805 --> 00:10:16,530
expecting an integer, given what we know
now, can crash our program.

169
00:10:16,530 --> 00:10:19,712
So you should go do an in lecture quiz
about users and

170
00:10:19,712 --> 00:10:23,368
their behavior and then come back.
Okay.

171
00:10:23,368 --> 00:10:26,987
The la-, we, by the way there are ways, of
course, to

172
00:10:26,987 --> 00:10:32,070
keep the program from crashing, we just
don't know those ways yet.

173
00:10:32,070 --> 00:10:35,990
And we won't learn them in this course.
We'll learn some.

174
00:10:35,990 --> 00:10:40,040
Data validation stuff, but not that
particular one, but as you

175
00:10:40,040 --> 00:10:45,440
explore C# programming more you'll learn
about something called exceptions, and

176
00:10:45,440 --> 00:10:48,815
we can actually handle the exception where
the user is

177
00:10:48,815 --> 00:10:52,600
a moron and doesn't enter an integer when
we asked for.

178
00:10:52,600 --> 00:10:53,670
For one.

179
00:10:53,670 --> 00:10:55,360
So let's actually do one more thing
though.

180
00:10:56,450 --> 00:11:06,260
Let's extract the first character of the
gamertag.

181
00:11:07,810 --> 00:11:10,680
This might be useful, if in fact we, you
know.

182
00:11:10,680 --> 00:11:13,800
Had a friend's list, and we wanted to sort
it into all the

183
00:11:13,800 --> 00:11:17,640
gamer tags of your friends that started
with g or j or whatever.

184
00:11:17,640 --> 00:11:20,140
And so this could be a useful thing.

185
00:11:20,140 --> 00:11:25,096
And we're going to actually use, well, we
know a string is a sequence of

186
00:11:25,096 --> 00:11:30,758
characters, so when we extract one, we'll
put it into a character variable.

187
00:11:30,758 --> 00:11:35,810
First character.

188
00:11:35,810 --> 00:11:37,646
Actually, let's make that an even better

189
00:11:37,646 --> 00:11:39,885
variable name, it's first gamer tag
character.

190
00:11:39,885 --> 00:11:40,600
[NOISE]

191
00:11:40,600 --> 00:11:46,836
And so it feels like, and there is.
It feels like there should be

192
00:11:46,836 --> 00:11:51,590
a way that we can extract from the gamer
tag a character at a particular location.

193
00:11:51,590 --> 00:11:52,560
And there is.

194
00:11:52,560 --> 00:11:53,950
We use these square brackets.

195
00:11:55,340 --> 00:11:59,063
And between the square brackets, we enter
the index of, the

196
00:11:59,063 --> 00:12:03,860
location in the sequence, of the character
we want to extract.

197
00:12:03,860 --> 00:12:05,628
And so, ignore everything you've ever

198
00:12:05,628 --> 00:12:09,830
learned about counting.
The first character is at location zero.

199
00:12:09,830 --> 00:12:12,920
Because programmers start counting at
zero, by gosh.

200
00:12:12,920 --> 00:12:15,540
And that's where the first character is.

201
00:12:15,540 --> 00:12:21,300
So we can extract a character using the
square brackets from the sea.

202
00:12:22,400 --> 00:12:23,870
Bad English sentence.

203
00:12:23,870 --> 00:12:26,795
From the sequence of characters that are
the string,

204
00:12:26,795 --> 00:12:30,090
we can extract a character using these
square brackets.

205
00:12:30,090 --> 00:12:30,738
And let's

206
00:12:30,738 --> 00:12:34,780
print that out as well just to show that
we've done that properly.

207
00:12:36,850 --> 00:12:39,776
But before I finish typing this and we run
it,

208
00:12:39,776 --> 00:12:44,250
go do the in lecture quiz about extracting
characters from strings.

209
00:12:50,970 --> 00:12:56,493
Okay, so first gamertag

210
00:12:56,493 --> 00:13:01,832
character.
Is this.

211
00:13:01,832 --> 00:13:04,890
I Ctrl-F5.

212
00:13:04,890 --> 00:13:07,600
That started with F, pay attention to
that.

213
00:13:07,600 --> 00:13:11,300
I'm level one.
And so the first gamer tag character is.

214
00:13:11,300 --> 00:13:15,786
In fact, F, just as we expected.
Okay,

215
00:13:15,786 --> 00:13:20,010
so that's it for our programming for
today.

216
00:13:20,010 --> 00:13:22,610
I'll Ctrl-S to save all that stuff.

217
00:13:23,770 --> 00:13:29,250
Before I finish, I want you to go do one
more in-lecture quiz.

218
00:13:29,250 --> 00:13:36,500
But before you do, I want to say that
strings are what's called immutable.

219
00:13:36,500 --> 00:13:39,790
So go take the in-lecture quiz to see if
you know what that means.

220
00:13:42,220 --> 00:13:43,000
Okay.

221
00:13:43,000 --> 00:13:50,020
Immutable actually means that once we have
a string object, we can't change it.

222
00:13:50,020 --> 00:13:53,548
We can form new strings from it, and we
can extract characters from it, and

223
00:13:53,548 --> 00:13:57,730
we can parse it into integers, and we can
do all kinds of other stuff.

224
00:13:57,730 --> 00:14:01,280
But once we've made a string object, it
can never be changed.

225
00:14:01,280 --> 00:14:04,970
It can't be mutated.
It is Immutable.

226
00:14:04,970 --> 00:14:06,820
Okay, so to recap for today,

227
00:14:06,820 --> 00:14:10,964
strings are sequences of characters,
they're really useful for

228
00:14:10,964 --> 00:14:15,150
getting and processing user input, and
strings are immutable.

229
00:14:16,180 --> 00:14:18,932
Next time, we'll take a look at doing

230
00:14:18,932 --> 00:14:23,090
some other useful string, useful things
with strings.