-
This is a, going to be a very short
section. I just want to add on an
-
additional test that happens to work very
well with the baby name data. So this is
-
going to be an alternative to the == that we were using before on the
-
baby name data. And I want to show you
these functions, which are called
-
startsWith and endsWith. And these work on
strings. And to explain it, I'll just look
-
at my code here. The way this works. Is,
all this is the same as we've seen before.
-
I opened the table, they have the loop,
they have the if-statement, where I'm
-
looking at each row. So we've just look
at the task left right here. So, the first
-
part of the task is the same as we've done
before. So, I say row.getField("name") so
-
I'm pulling the name out of the row, and
that's a string. And previously we've done
-
examples like, oh, is it equal to "Abby" or
"Robert" or whatever, with the ==-test.
-
The way these startsWith and
endsWith -functions work, is the syntax is
-
kind of funny. It has a dot... And you
just tag it right on to the right hand
-
side of the name so I get out the name and
I immediately say .startsWith. And
-
then, inside of the parenthesis for the
startsWith, you could just say anything.
-
So here I say "Ab". And what that tests is,
does the name begin with the letters "Ab"?
-
And if it does its true, the startsWith
is true, and if it doesn't its false. So
-
it kinda reads. You know, does, does, does
the name start with "Ab"? So, let's just
-
try it. So if I just run this, what you'll
see is it's gone through the 2,000 rows,
-
and it's just picked out, we're gonna see
they all start with "Ab". It's just; it was
-
true, for all of those. So whatever I type
here. We're gonna get rows where the name
-
begins with that, So, I can type... I
don't know, we'll reverse it. I can type
-
in "Ba", oh, there's not that many. Or I
can type maybe just uppercase "A", not two
-
letters. We're gonna get quite a lot,
quite a lot of names, begin with "A". If I
-
type a lower case "a" here, And I run. We
actually get no output. And what's going
-
on there is in the computer the upper
case "A" and the lower case "a", those are
-
just two different letters. And so in this
data the names all begin with an upper
-
case letter and then all the other letters
are lower case. So startingWith("a")
-
- that's a subtlety that has
zero results, where's if I put an upper
-
case "A" here and run it then we get them all.
Right, and you can even see how yea, they
-
begin with an upper case letter then later
letters are lower case. So you just need
-
to know that on the test. What else was I
going to do here? Let's try "Z". So if I
-
look for startsWith("Z"), then we get all
the names; there's a few there. Or I
-
could narrow it. I could say, oh well, maybe it has to start with "Za". Then we get fewer. And maybe I could say "Zai".
-
Then we get down to just four, And
then I could say it like, I'll say it like
-
"Zai"... I don't know, "x". Then I run it, oh
then I get naught. So its poss, it's fully
-
possible to write a test where it's just
false. For all 2000 rows and then it just
-
prints nothing. So I'll just back it to "Zai", and then we just do those. So that
-
the startsWith function, And for these
first examples, we're always going to use
-
it pretty much this way. So we go the row
for the baby data, we get the name out,
-
and then we immediately say .startsWith, to check the left side of the name.
-
So, as you might guess, there's also a
.endsWith that tests the other, the right
-
hand side of the name. So if I say,
endsWith("z"), I run that, oh gees,
-
surprisingly few of those so that picks
up, apparently there's only three names in
-
this whole data set that end with "z". So I
could say, or I could take multiple
-
letters. I could say, endsWith("ly"). So a
few more Or I, I'll try endsWith("la" ). And
-
I'll run that. Oh, so then there's quite a
few of those. You can see, I think these
-
are all girl names too. I mean, that,
that's just culturally the pattern in
-
English that, girl name, or "la" is,
supposed to be a girl's name. So, these
-
are, this is just two addit-, just two
additional functions. And we'll, as I was
-
saying, we'll t end to use them for the
names in the baby data set, baby data set.
-
But they're, they're gonna allow us to do
some more interesting problems.