"Java Data Structures - Retired" was retired on May 31, 2019. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Let's explore Strings and how to concatenate them together
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I like to imagine a string just like
a banner you might see at a party,
0:00
with each letter strung together.
0:04
That's really what a string is, right?
0:06
It's a series of characters.
0:08
The string data type provides some
very handy methods that I'd like
0:10
you to get familiar with.
0:14
After you create a string,
it cannot be changed.
0:15
This is what is known as immutable or
impossible to modify.
0:19
I'll remind you of this as we
look at some of these examples.
0:23
Why don't we pop open a shell and
I can show you what I'm talking about.
0:26
So string literals, they can be made
with either single or double quotes.
0:29
And I can imagine that you
might say something like this.
0:35
You could say, I cannot understand
why you need two options for quotes.
0:38
Totally understand why you
might say something like that.
0:46
But that's probably a little bit formal,
isn't it?
0:50
We should probably use a conjunction.
0:52
How about we say something
like I can't understand.
0:54
Oops, that's a problem, right?
1:00
We've got a quote inside of the quote.
1:03
We got a single quote.
1:06
We're trying to use a single quote.
1:07
Now, one option is that you
can actually make it so
1:09
the quote is ignored by doing
something called escaping.
1:12
So the backslash starts what is
known as an escape sequence.
1:16
So the backslash and then a quote
basically tells the interpreter to treat
1:20
that quote like a character instead of
treating it like part of the syntax,
1:24
right?
1:29
So if I do, can't understand,
now, we'll see.
1:29
But I mean,
this is kind of ugly, isn't it?
1:36
This backslash t.
1:38
Are you glad there's two
types of quotes yet?
1:40
Actually, look, the repl fixed it so
you can use quotes like that.
1:42
So if you use double quotes,
1:45
then you could very easily use
a single quote inside of it.
1:47
So now we can say I can't, right?
1:51
There's a nice string.
1:55
And actually escape
sequences are great for
1:58
adding blank lines inside of your string.
2:00
So there's a special one that you'll see,
if we come over here and we go up.
2:02
Let's get this, I can't, back here.
2:08
Let's say I can't.
2:10
And then we want to add a new line.
2:12
And so that escape sequence to add
a new blank line is \n for newline.
2:14
So we'll add to two blank lines and
we'll say, I can't even.
2:19
And the repl here is playing tricks on us.
2:27
It's trying really hard to keep
things on one line for us.
2:29
But actually if you print that string, so
we'll go ahead and we'll say print, and
2:32
then we'll get back the result
that was just there, right?
2:35
So we use the underscore.
2:37
So you'll see that it is Actually,
I can't, new line, new line, even.
2:39
There's our new lines.
2:42
My wife says that to me all the time while
shaking her head about my dad jokes.
2:44
You're not the only one.
2:48
I suppose I should quote her though,
right?
2:49
So I would say she said, I can't.
2:52
So now what?
2:59
Now, we've got a single quote and
a double quote in here.
3:00
Actually, triple quotes allow
you to start the string.
3:05
So we could say, she said, I can't.
3:11
And a nice thing about triple quotes is it
allows you to have spaces in your string.
3:16
So we can press like this and
see those triple dots over there?
3:21
That means it's waiting for me to.
3:23
It's waiting for those final three quotes.
3:25
So we can say I can't even.
3:27
So she said that it's still.
3:30
There's another quote there, right?
3:32
So I'm ending that quote, and then it's
waiting for three quotes to end it.
3:33
So there we go.
3:40
And if I say go up a couple times here,
we say print.
3:41
She said, I can't even.
3:45
That's what she said.
3:47
All right, so
now that we got creation out of the way,
3:49
let's look at combining
some strings together.
3:52
What if I had a string
like this word here?
3:55
Chocolate, right?
3:58
Now, we can actually combine it together
with another string using a plus sign.
4:00
So I can say chocolate + marshmallow.
4:06
And you'll see what it returned was
a brand new string with the two
4:12
words pushed together.
4:17
This is called concatenation.
4:19
Now, it's important to remember when
you're concatenating strings to include
4:21
the proper spacing, otherwise it
will be slammed together like this.
4:25
So what we want is probably
some more like this.
4:29
Chocolate + space and
4:33
marshmallows.
4:39
There we go.
4:43
That feels good.
4:44
And of course,
4:45
we can store that new string that
was created in a variable, right?
4:45
So we could say dessert =
4:49
chocolate + and marshmallows.
4:54
So that was a new string created,
and then we labeled it with dessert.
5:01
And we can use that variable
to create a brand new one.
5:05
This is called reassigning.
5:09
So we'll say dessert equals dessert + and
graham crackers.
5:10
And now that might look like we
changed the variable dessert.
5:23
But what happened was that this statement,
5:27
dessert + and
graham crackers created a new string and
5:30
we removed our already existing label and
put it on the new string.
5:33
We reassigned it.
5:37
The old string, since it didn't have
a label, is essentially thrown away,
5:39
like those leftovers in my office fridge.
5:43
Remember, we didn't actually change the
original string because we can't, right?
5:45
And that's because strings are immutable.
5:51
This appending of more text to
the end of a string is pretty common.
5:54
So there's a shortcut
called in place addition.
5:58
So if we say dessert,
we can say plus equals.
6:01
And basically that's
just like the line above.
6:05
It's saying dessert equals dessert plus.
6:07
And then whatever we do.
6:09
So we say plus equals yum.
6:11
And if we take a look, chocolate
marshmallows and graham crackers, yum.
6:15
So that needs some exclamation points,
am I right?
6:21
And I want some more than just a couple.
6:25
I don't wanna just add
a whole bunch of them myself.
6:27
I'd rather do that in code.
6:29
So that's where the asterisk
comes into play.
6:31
So check this out.
6:33
If I want to repeat the string, well,
here's the string exclamation point.
6:35
I can then do a star for multiplication.
6:40
And I say do that 20 times, awesome.
6:42
In addition to exclamations,
this is really handy for
6:46
trying to draw layouts in text.
6:49
So let's append those.
6:51
So we'll say dessert and
we'll do an in place addition.
6:53
So dessert, basically that's
dessert equals dessert plus.
6:55
And we'll say the exclamation
point times 20.
6:59
And there we go.
7:03
Now, before I append some more
string information into your brain,
7:05
let's take a quick break and
return to talk some more about strings.
7:09
We'll talk about various handy
methods that a string provides.
7:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up