1
00:00:08,580 --> 00:00:12,520
Last time we talked about the string data
type, and just

2
00:00:12,520 --> 00:00:17,520
as a reminder, a string is simply a
sequence of Unicode characters.

3
00:00:17,520 --> 00:00:19,510
This time we're going to look at a couple
of

4
00:00:19,510 --> 00:00:24,620
more interesting processing things that we
can do with strings.

5
00:00:24,620 --> 00:00:28,810
As a reminder, here are the module
learning objectives for Module Six.

6
00:00:28,810 --> 00:00:31,450
And we're really working on the second
one, in this case.

7
00:00:31,450 --> 00:00:33,790
Where we're taking a look at string
processing.

8
00:00:38,270 --> 00:00:43,770
So you might ask yourself, what might we
want to actually do with strings?

9
00:00:43,770 --> 00:00:48,740
We've already seen how we can read them in
using console.read line.

10
00:00:48,740 --> 00:00:52,640
We've already seen that we can convert
them into a value type.

11
00:00:52,640 --> 00:00:53,730
We did that last time.

12
00:00:55,210 --> 00:00:59,370
We might need to look for a particular
character in the string.

13
00:00:59,370 --> 00:01:04,330
So for example, it is very common for
people to export

14
00:01:04,330 --> 00:01:09,800
CSV files, comma separated value files,
from Excel or something like that.

15
00:01:09,800 --> 00:01:11,600
You could also separate them by tabs.

16
00:01:11,600 --> 00:01:15,910
You can use any separator character you
want when you export.

17
00:01:15,910 --> 00:01:19,750
So it might be the case, though, that we
want to write a program that reads in

18
00:01:19,750 --> 00:01:22,990
such a file and finds the locations of

19
00:01:22,990 --> 00:01:26,390
those separators so that we can do
interesting stuff.

20
00:01:26,390 --> 00:01:28,230
So we might have to find a particular
character.

21
00:01:29,800 --> 00:01:33,850
And in fact, once we've found that
particular character, we might

22
00:01:33,850 --> 00:01:38,040
want to extract part of that string into a
new string.

23
00:01:38,040 --> 00:01:43,440
To, to sort of gather the information from
each line in that CSV file.

24
00:01:43,440 --> 00:01:48,970
And we might do something else.
And that's so vague, right?

25
00:01:48,970 --> 00:01:50,380
What something else?

26
00:01:50,380 --> 00:01:53,100
Whatever you discover you need to do with
strings.

27
00:01:53,100 --> 00:01:55,260
And the way we move

28
00:01:55,260 --> 00:01:56,510
forward as programmers.

29
00:01:56,510 --> 00:02:00,910
In, programming, in general, and game
development, in particular.

30
00:02:00,910 --> 00:02:04,050
Is, we can't learn everything as
beginners.

31
00:02:04,050 --> 00:02:05,340
We learn what we need.

32
00:02:05,340 --> 00:02:08,040
And then, as we need to learn new stuff,
we learn it.

33
00:02:08,040 --> 00:02:11,850
And so, you may discover lots of things
you want to do with strings.

34
00:02:11,850 --> 00:02:13,900
And when the time comes, you'll look at

35
00:02:13,900 --> 00:02:17,080
the documentation and you'll try to figure
it out.

36
00:02:17,080 --> 00:02:20,525
Okay, so let's actually start with an

37
00:02:20,525 --> 00:02:26,120
in-lecture quiz where I ask you how we can
do that find a particular

38
00:02:26,120 --> 00:02:29,810
character in the string, and then we'll
actually go do some code to do it.

39
00:02:32,890 --> 00:02:35,220
I will open up the project that I already
created.

40
00:02:35,220 --> 00:02:35,220
>>

41
00:02:35,220 --> 00:02:36,680
[SOUND]

42
00:02:36,680 --> 00:02:37,180
>> And

43
00:02:39,280 --> 00:02:42,170
we'll say, read in CSV string.

44
00:02:45,290 --> 00:02:51,750
Prompts to remember are console.right with
no politeness whatsoever.

45
00:02:53,400 --> 00:02:59,306
Enter name, well okay, we can't use Worte.
It has to be right.

46
00:02:59,306 --> 00:03:04,560
Enter name and percent.

47
00:03:04,560 --> 00:03:07,350
So imagine this is for a class or
something like that.

48
00:03:07,350 --> 00:03:10,530
And it makes, if we're expecting a
particular

49
00:03:10,530 --> 00:03:14,130
format from the user, even though we know
users are going to mess with us no matter

50
00:03:14,130 --> 00:03:19,955
what, we should tell them the format.
So let's say, name,

51
00:03:19,955 --> 00:03:26,030
percent like that.
So there's out prompt;

52
00:03:26,030 --> 00:03:32,090
and we'll read in the CSV string using
console read line.

53
00:03:37,750 --> 00:03:42,910
Now, once we have a comma separated
string, we can extract values from that

54
00:03:42,910 --> 00:03:47,775
string and the first thing we're going to
do is find out where the comma is, so

55
00:03:47,775 --> 00:03:48,390
[NOISE]

56
00:03:48,390 --> 00:03:49,930
find comma location

57
00:03:52,910 --> 00:03:58,365
and we can do that, let me just declare a
variable here.

58
00:03:58,365 --> 00:03:58,850
[NOISE]

59
00:03:58,850 --> 00:04:01,660
We can do that by using, hopefully you've
got this right on the

60
00:04:01,660 --> 00:04:07,110
induction quiz we can use that, you do
that using the index of method.

61
00:04:07,110 --> 00:04:08,230
So all we have to do is

62
00:04:08,230 --> 00:04:14,980
say csvstring.IndexOf and we'll look for
the comma

63
00:04:18,180 --> 00:04:22,750
without any extra parenthesis, hopefully.
Okay.

64
00:04:22,750 --> 00:04:25,560
And, by the way, I did this as a string
but

65
00:04:25,560 --> 00:04:28,230
we can, of course, look for a single
character as well.

66
00:04:28,230 --> 00:04:30,260
By using single quotes around the literal

67
00:04:30,260 --> 00:04:32,650
rather than double quotes around the
literal.

68
00:04:32,650 --> 00:04:37,270
So let's actually use the debugger to
explore a little bit more

69
00:04:37,270 --> 00:04:42,170
how indexOf works by setting a break
point, and I'll run it.

70
00:04:43,330 --> 00:04:47,980
And first I'll enter something like My
last name or

71
00:04:47,980 --> 00:04:53,100
name Bob, and my percent, I'm stupid, so
we'll say zero.

72
00:04:53,100 --> 00:04:58,600
Think for a moment about what you think
indexOf will return

73
00:04:58,600 --> 00:05:02,530
and then we can look at comma location and
it's three.

74
00:05:02,530 --> 00:05:08,740
So, remember we start at zero, character
zero is B, character one is O.

75
00:05:08,740 --> 00:05:12,660
Character two is B and the comma is at
location three.

76
00:05:13,750 --> 00:05:15,600
The other thing we can do, I'll press F5

77
00:05:15,600 --> 00:05:18,390
and that will just finish the program for
me.

78
00:05:18,390 --> 00:05:21,140
I'm going to, and notice by the way that
because I'm

79
00:05:21,140 --> 00:05:25,120
using the debugger in this particular
case, I'm actually using F5.

80
00:05:25,120 --> 00:05:28,690
In this console application instead of
Cltr+F5.

81
00:05:28,690 --> 00:05:31,700
That's why I said when I, that's why when
I

82
00:05:31,700 --> 00:05:33,990
said go all the way to the end, it
actually

83
00:05:33,990 --> 00:05:36,080
just flashed that screen.

84
00:05:36,080 --> 00:05:38,220
Okay, I'm going to do it one more time and

85
00:05:38,220 --> 00:05:40,480
then I'm going to enter something without
any comma at all.

86
00:05:41,630 --> 00:05:43,160
And when we get to the break point.

87
00:05:45,960 --> 00:05:49,326
When we get to the break point, the comma
location is negative one.

88
00:05:49,326 --> 00:05:53,540
So if indexOf finds what we're looking
for, it returns

89
00:05:53,540 --> 00:05:57,440
the location of the first occurrence of
what we're looking for.

90
00:05:57,440 --> 00:05:59,780
So if we'd had multiple commas in our
string it wouldn't

91
00:05:59,780 --> 00:06:03,300
matter, it still would give us the
location of the first one.

92
00:06:03,300 --> 00:06:06,850
The other thing that happens is, if what
we're looking for does

93
00:06:06,850 --> 00:06:11,510
not appear in the string, then it just
tells us, well it returns

94
00:06:11,510 --> 00:06:13,700
negative one, okay.

95
00:06:13,700 --> 00:06:15,020
So now we know where I'm going to get rid

96
00:06:15,020 --> 00:06:19,490
of the break point, and I'll F5 to finish
off.

97
00:06:19,490 --> 00:06:23,235
So now we've found the location let's
extract.

98
00:06:23,235 --> 00:06:25,410
[NOISE]

99
00:06:25,410 --> 00:06:31,910
Name and percent.
We'll also print name and percent

100
00:06:31,910 --> 00:06:38,010
and then of course that blank line that we
know and love.

101
00:06:39,030 --> 00:06:42,220
Okay before we extract.

102
00:06:42,220 --> 00:06:45,260
What we need to extract, go to the in
lecture quiz

103
00:06:45,260 --> 00:06:47,200
to tell me if you have a good guess about
that.

104
00:06:51,350 --> 00:06:52,240
All right.

105
00:06:52,240 --> 00:06:58,150
So, we actually extract using sub-string,
and the sub-string takes a piece of

106
00:06:58,150 --> 00:07:01,540
the string we're extracting from it and
puts it into a new string.

107
00:07:01,540 --> 00:07:04,920
Remember, strings are immutable, so we
can't modify the string

108
00:07:04,920 --> 00:07:08,570
we have, but we can create new strings
from it.

109
00:07:08,570 --> 00:07:13,380
So I'll say string name is equal to, and
now

110
00:07:13,380 --> 00:07:16,780
we need to use sub string on our csv
string,

111
00:07:21,870 --> 00:07:26,720
to extract a piece of the string.
Now we've talked about overloads.

112
00:07:26,720 --> 00:07:28,860
Sub string has two overloads.

113
00:07:28,860 --> 00:07:34,850
One, We give it the Start Index and it
goes all the way to the end.

114
00:07:34,850 --> 00:07:37,740
It starts at whatever we've provided as
that Start Index.

115
00:07:37,740 --> 00:07:39,550
Remember, the thing between those square

116
00:07:39,550 --> 00:07:42,690
brackets when we accessed a single
character.

117
00:07:42,690 --> 00:07:45,560
In the sequence of characters that are the
string.

118
00:07:45,560 --> 00:07:47,050
Is called an Index.

119
00:07:47,050 --> 00:07:48,880
So we can give it the Index.

120
00:07:48,880 --> 00:07:49,990
And it'll go all the way to the end of

121
00:07:49,990 --> 00:07:53,650
the string but that's definitely not what
we want here.

122
00:07:53,650 --> 00:07:56,290
What we want here is we want to start at
the

123
00:07:56,290 --> 00:07:59,430
beginning of the string and go up to the
comma.

124
00:07:59,430 --> 00:08:01,960
So we're going to use the otherr over
overload.

125
00:08:01,960 --> 00:08:06,940
We're going to use the overload where we
provide a start index and a length.

126
00:08:06,940 --> 00:08:09,540
So, the start index is zero, because we
want to

127
00:08:09,540 --> 00:08:12,680
start extracting the name from the
beginning of this string,

128
00:08:12,680 --> 00:08:16,350
and the question is how long?
How many characters do we need to grab.

129
00:08:16,350 --> 00:08:21,540
And it turns out that having zero based
strings and having index of work the way

130
00:08:21,540 --> 00:08:27,650
it does is awesome because the length is
actually comma location.

131
00:08:27,650 --> 00:08:30,320
We want to extract Three characters.

132
00:08:30,320 --> 00:08:34,440
The B, the O, and the B, from the example
that I used.

133
00:08:34,440 --> 00:08:37,790
So, I'm going to print this one to show
that we've extracted

134
00:08:37,790 --> 00:08:39,580
that properly before we move on.

135
00:08:43,290 --> 00:08:43,970
So the name

136
00:08:47,270 --> 00:08:48,188
is that.

137
00:08:48,188 --> 00:08:52,500
We will Control+F5 now because I'm not
debugging anymore.

138
00:08:52,500 --> 00:08:54,070
I'll still do Bob.

139
00:08:54,070 --> 00:08:56,430
I'll do ten now because bob's getting
smarter over time.

140
00:08:57,770 --> 00:08:59,070
And the name is Bob.

141
00:08:59,070 --> 00:09:01,540
So it extracted it properly using sub
string.

142
00:09:02,740 --> 00:09:06,050
The next thing we want to do is let's
extract that percent.

143
00:09:06,050 --> 00:09:07,950
Now you might be tempted to say string

144
00:09:07,950 --> 00:09:11,120
percent because I know I'm doing string
processing.

145
00:09:11,120 --> 00:09:12,910
But that would be wrong.

146
00:09:12,910 --> 00:09:15,220
It would be wrong morally.
You're a bad person.

147
00:09:15,220 --> 00:09:15,970
Don't do that.

148
00:09:15,970 --> 00:09:22,640
So, in fact percent is a float, so let's
store percent as a float.

149
00:09:24,090 --> 00:09:27,840
We'll do csvstring substring.

150
00:09:29,240 --> 00:09:31,610
And this time we actually want that first
overload.

151
00:09:31,610 --> 00:09:36,760
Where we start at a particular place and
go the end of the string.

152
00:09:36,760 --> 00:09:38,500
And that particular place

153
00:09:38,500 --> 00:09:42,580
is not comma location, it's comma location
plus one.

154
00:09:42,580 --> 00:09:44,760
And you might find that you need to

155
00:09:44,760 --> 00:09:49,600
actually like use those archaic things,
pencil and paper.

156
00:09:49,600 --> 00:09:52,150
And write down a string and look at it,

157
00:09:52,150 --> 00:09:55,640
and number all of the characters starting
at zero.

158
00:09:55,640 --> 00:09:58,480
And see why it needs to be comma location
plus 1.

159
00:09:58,480 --> 00:10:00,190
You should check that out.

160
00:10:00,190 --> 00:10:04,120
However, I am getting a compilation error
here, and I can't

161
00:10:04,120 --> 00:10:07,050
implicitly convert string to float.

162
00:10:07,050 --> 00:10:11,520
You should say, wow I remember seeing that
last time.

163
00:10:11,520 --> 00:10:15,190
Because if I want to convert a string to a
numeric data type.

164
00:10:15,190 --> 00:10:18,600
Then I need to parse it, and this time
it's not an int it's

165
00:10:18,600 --> 00:10:23,890
a float but that's okay, we'll just do
float parse instead of int parse.

166
00:10:25,270 --> 00:10:27,490
And we'll print it out again, well, not
again

167
00:10:27,490 --> 00:10:27,530
[LAUGH]

168
00:10:27,530 --> 00:10:30,020
we'll print this out for the very first
time

169
00:10:30,020 --> 00:10:32,030
but we will print out the percent as well.

170
00:10:34,450 --> 00:10:37,500
Just to demonstrate that we have, in fact,
extracted it properly.

171
00:10:42,300 --> 00:10:44,670
Like so.
And so Control+F5.

172
00:10:44,670 --> 00:10:48,840
I'll enter Bob comma and this time I'll do
20.0

173
00:10:48,840 --> 00:10:52,300
because Bob realizes a percent should be a
float as well.

174
00:10:52,300 --> 00:10:57,310
And it prints out Bob and 20, and we're in
good shape.

175
00:10:58,480 --> 00:11:02,200
Okay.
So here's the thing.

176
00:11:02,200 --> 00:11:04,000
We have, and, and that's really it.

177
00:11:04,000 --> 00:11:07,420
That's the exploration of the string
processing that we're going to do

178
00:11:07,420 --> 00:11:09,600
for today.
Index of.

179
00:11:09,600 --> 00:11:12,580
There's a corresponding method called last
index

180
00:11:12,580 --> 00:11:15,870
of, and substring are three methods that

181
00:11:15,870 --> 00:11:20,850
you are likely to use to process strings
regularly to do the stuff you need.

182
00:11:20,850 --> 00:11:22,700
When you discover there's something else
you need to

183
00:11:22,700 --> 00:11:26,000
do to strings, you go read the string
documentation.

184
00:11:26,000 --> 00:11:28,180
Each method tells what it does.

185
00:11:28,180 --> 00:11:30,600
And you find the one that will work for
you.

186
00:11:31,650 --> 00:11:32,680
So that's

187
00:11:32,680 --> 00:11:36,790
it for our string module, Module Six.

188
00:11:36,790 --> 00:11:40,800
So next time in the next lecture we will
move on to Module Seven.

189
00:11:40,800 --> 00:11:44,560
Where we will start talking about how we
can make decisions in our programs.