YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Combining Strings Together

Get Embed Code
13 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. When dealing with strings in Java,
  2. an important concept to understand
    is string concatenation.
  3. Now that's a really big word, but
  4. it just means we're joining character
    strings together end to end.
  5. If this is a string and
    this is a string,
  6. you can combine them by concatenating
    them to make an even longer string.
  7. To concatenate these strings together,
    we use the plus operator.
  8. This is the same addition
    symbol that we know from math.
  9. Just like you can add numbers together,
    you can concatenate strings together.
  10. Let's look at an example.
  11. Say I have three different strings,
    one string literal says I need,
  12. another string literal
    says 2 cups of coffee,
  13. and another string
    literal says on Monday.
  14. I can use the plus symbol to
    concatenate all these strings together.
  15. That forms a ginormous string that
    says I need2 cups of coffeeon Monday.
  16. Whenever I see something like this,
    I imagine the plus symbols are gone, and
  17. I imagine the quotes are gone, and
  18. I just imagine literally squishing
    all of these things together.
  19. And when I say squished,
    we're really squishing them together.
  20. There's even no extra space in
    between this string and this string.
  21. If you want to add a space here,
  22. you would have to explicitly add a space
    in this string literal at the end of it,
  23. or you add a space at the beginning
    of this string literal.
  24. Same with coffeeon Monday.
  25. I want a space here, so I'd have to
    either add it at the end of this string
  26. or the beginning of this string.
  27. I added a space here and
    a space here, so
  28. when I concatenate all of this together,
  29. I squish them together, and the sentence
    comes out correct like this.
  30. There's a space here and a space here.
  31. Adding spaces in the right place is
    a little bit tricky because you have
  32. the quotation marks everywhere and
    the plus symbols, and
  33. there's even spaces
    around the plus symbol.
  34. But these spaces around the plus
    symbol don't contribute
  35. to the overall display string.
  36. The space must be inside
    the double quotes.
  37. Here's an example of string
    concatenation in our app.
  38. I'm going to change the text so
  39. that it says "Amount due " + "$10".
  40. I'm concatenating this string
    literal with this string literal.
  41. When I run it on my device, and
  42. then I hit the order button,
    then I see Amount Due $10.
  43. You can also concatenate strings
    with integers like I have here.
  44. Before, the ten was in quotes so
  45. that was a string representation
    of the number ten.
  46. But here I just have 100 without quotes,
    so this is the integer value for 100.
  47. If I concatenate a string
    with an integer,
  48. then it immediately turns this
    whole thing into a string.
  49. If I hit the Order button, then I
    see $100 showing up on the screen.
  50. In a moment, I'll have you play around
    with string concatenation to try
  51. different values.
  52. You could get compile errors,
    so be careful of those.
  53. If I forget a closing quote,
    I could get an error.
  54. In a moment, I'll have you play
    around with string concatenation and
  55. try different values.
  56. According to Android
    code style guidelines,
  57. we should have a space before and
    after each operator.
  58. And this string concatenation
    operator counts as an operator.
  59. Now it's your turn to
    practice in your app.
  60. Experiment with combining different
    strings using the plus operator.
  61. You can also combine it with
    integer literal values as well.
  62. Once you feel comfortable
    with string concatenation,
  63. I want you to answer these questions.