< Return to Video

Strings (4 mins)

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

English subtitles

Revisions