Section 3 - String Concatenation

3A.3.1 The Concatenation Operator

The concatenation operator is used to join together two or more strings

string acceptanceSpeech, thxFox, outputString;

acceptanceSpeech = "I'd like to thank ";
thxFox = "everyone at Fox and FBC.";

outputString = "First of all " + acceptanceSpeech + thxFox;

We can concatenate two strings as long as one of them is a variable (or as we often say, a string object). You cannot directly concatenate two string literals.  In other words, this is illegal:

outputString = "First of all " + "I'd like to thank "; // compiler error!

However, you can do a trick that will allow you to use the concatenation operator on these literals:

outputString = string("First of all ") + string("I'd like to thank ");  // works!

This so-called trick has a deeper and more universal meaning that we will discuss in a few weeks.  For now, just know that you can convert a string literal to something that acts like a string object by just placing it inside the parentheses of string().

The next program will help you if you ever receive  an Oscar at the Academy Awards.  It will demonstrate use of the concatenation operator, +, as well as the new string data type that we just presented.  We will create a speech by piecing together certain phrases. 

Have a look:

#include <iostream>
#include <string>
using namespace std;

int main()
{
// declare some string references (variables)
string thxMom, thxAgent, thxFox;
string outputString;

// for fun, create the String object directly in the declaration
string acceptanceSpeech = "I'd like to thank ";

// create the rest of the  string objects to use
// in your speech.
thxMom = "my Mother, Ethel, and wife Kitty.";
thxFox = "everyone at Fox and FBC.";
thxAgent = "my agent and everyone at Paradigm.";

// stand up at the podium and get settled ...
// for this use String Literals directly in the
// output statements.
cout << "I didn't really expect to win ...\n\n";
cout << "I don't even have a speech prepared!\n\n";
cout <<  "Anyway ...\n\n";

// now finally start to thank people.
outputString = "First of all " + acceptanceSpeech + thxFox + "\n";
cout << outputString;

outputString = "Next, " + acceptanceSpeech + thxAgent +  "\n";
cout << outputString;

outputString = "But mostly, " + acceptanceSpeech + thxMom +  "\n\n";
cout << outputString;

return (0);
}

The screen output looks like this:

console output

This program has a few variations of what we learned:

Paste this into your compiler IDE and run it.  It's a fun little program and you can use it as a basis for doing homework assignments.

3A.3.2 Automatic String Literal Concatenation

If you need a long literal string (i.e., a bunch of characters inside quotes) to continue on to the next line, you can't just hit the return/enter key and continue typing the string. Remember, I said that whitespace and new lines are insignificant everywhere except inside literal strings.  Here is a way around this:

int userValue;

cout << "Enter an integer number and I'll tell you: "
" whether it's even or odd: ";

cin >> userValue;

So the trick is to close the quote on one line, then anywhere on the next line open a new quote. The two strings will be concatenated into one for you by the compiler. Problem solved.  This technique applies only to string literals, not string variables.

decorative flowers