"Python Basics (2015)" was retired on June 22, 2018. 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
Well done!
You have completed C# Basics!
You have completed C# Basics!
Preview
Let's learn how to join strings together using concatenation.
- Our printed question is running up against the space where the user types their answer.
- We need to add a space onto the end of the user
question. - We can do this by taking the
questionstring, and concatenating, or joining, a string consisting of a single space onto the end of it.
// We can concatenate strings.
Console.WriteLine("a" + "b");
Console.WriteLine("some words" + "more words");
// If we want a space between the joined strings, we need
// to add it ourselves.
Console.WriteLine("some words" + " " + "more words");
// We can concatenate strings stored in variables.
string myString = "a string";
Console.WriteLine(myString + " abc");
// Notice that simply concatenating onto a string stored
// in a variable doesn't change the value in the variable.
Console.WriteLine(myString);
// To update the variable, we need to assign the concatenated
// value back to it.
myString += " abc";
Console.WriteLine(myString);
myString += " def";
Console.WriteLine(myString);
- Using string concatenation to fix our
askmethod:
static string Ask(string question)
{
Console.Write(question + " ");
return Console.ReadLine();
}
- Let's print what the user entered so they can confirm it's correct.
static void Main()
{
Console.WriteLine("Welcome to the cat food store!");
string entry = Ask("How many cans are you ordering?");
Console.WriteLine("You entered" + entry + "cans");
}
You entered11cans
- Oops! We need to add spaces surrounding
answer, so fix that:
static void Main()
{
Console.WriteLine("Welcome to the cat food store!");
string entry = Ask("How many cans are you ordering?");
Console.WriteLine("You entered " + entry + " cans");
}
You entered 11 cans
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
In our cat food store program, our printed
question is running up against the space
0:00
where the user types their answer.
0:05
To fix this, we need to add a space
on to the end of the user question.
0:07
We can do this by taking the question
string and concatenating or
0:11
joining a string consisting of
a single space on to the end of it.
0:15
To concatenate two strings,
you just type a plus sign between them.
0:25
So this code here we'll join
the strings A and B together and
0:31
this would join the string some words and
more words together.
0:34
Let me save this and run it.
0:37
And here's the result.
0:44
You can see that the string some words
is smashed together with more words.
0:46
If we want a space between
the joint strings,
0:52
we'll need to add it ourselves by
a concatenating a third string in there.
0:54
Let me save this and re-run it.
0:59
There we go, and now there's a space.
1:04
We can also concatenate
strings stored in variables.
1:10
So let's declare a new variable named
myString and storeString in it.
1:13
And now, let's concatenate that
together with another string.
1:18
Let me save that.
1:23
And let's try running it,
there's the string from our variable,
1:27
and here's the string we
concatenated onto it.
1:32
One important thing to note, though,
is that simply concatenating onto
1:42
a string stored in a variable doesn't
change the value in the variable.
1:45
So, if we were trying to print
the content of my string again,
1:49
let's try that real quick.
1:52
You can see that my string still just
contains the value we originally assigned
1:56
to it, a string.
2:00
There's no abc on the end.
2:01
To update the value in the variable,
2:08
we need to assign the concatenated
value back to it.
2:09
We can do that with the += operator.
2:13
This will concatenate a new string
onto the string currently contained
2:15
in the myString variable.
2:20
And then assign that new
concatenated value back to myString.
2:22
So let's try printing that updated value,
save this and try running it.
2:26
And now you can see that myString
contains the updated value, a string abc.
2:34
I can even concatenate additional
strings onto the updated strings.
2:41
So let's add def and
2:45
assign that back to myString and
then print the resulting value.
2:47
And let's try running this,
and you can see that, first,
2:55
we updated myString so
that it contained a string abc, and
2:59
then we concatenated an additional
string onto that, a string abc def.
3:02
This is what the myString variable
contains at the end of the program.
3:08
So by assigning concatenated
strings back to the same variable,
3:13
you can keep building up longer and
longer strings.
3:16
Let's use string concatenation
to fix our ask method.
3:18
We'll simply take the value that we're
passing to the write method, and
3:23
concatenate a space onto the end of it.
3:28
That will cause there to be a space
between the question that gets asked and
3:31
the answer the user types.
3:35
So make sure to save that,
and let's try running it.
3:41
How many cans are you ordering?
3:45
Let's say 55, and
there's a space there now.
3:47
Right now, we're simply printing
the value the user enters so
3:52
we can debug it, but it would be helpful
for the user to know what they entered, so
3:54
let's make this a little more user
friendly and convert this into a sentence.
3:59
We'll do that using string
concatenation again.
4:04
We'll say you entered, As a string,
4:06
and then concatenate that together
with the value in the entry variable.
4:12
And concatenate that with
cans to end the sentence.
4:15
Save that let's try running it.
4:21
And whoops it joins the value we entered
together with the rest of the sentence but
4:27
theres no spaces surrounding
the value we entered.
4:32
So let's fix that.
4:36
We simply need to add spaces onto the end
of the string we're adding before
4:38
the entry and onto the start of
the string we're adding after the entry.
4:44
Let's save that.
4:49
And try running it again.
4:52
Okay, that looks much better.
4:56
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