C++ Strings
C++ Strings
Example
Create a variable of type string and assign it a value:
To use strings, you must include an additional header file in the source code,
the <string> library:
Example
// Include the string library
#include <string>
String Concatenation
The + operator can be used between strings to add them together to make a
new string. This is called concatenation:
Example
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
Example
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
Append
A string in C++ is actually an object, which contain functions that can
perform certain operations on strings. For example, you can also concatenate
strings with the append() function:
Example
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
Example
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer)
Example
string x = "10";
string y = "20";
string z = x + y; // z will be 1020 (a string)
String Length
To get the length of a string, use the length() function:
Example
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
Tip: You might see some C++ programs that use the size() function to get
the length of a string. This is just an alias of length(). It is completely up to
you if you want to use length() or size():
Example
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.size();
Access Strings
You can access the characters in a string by referring to its index number
inside square brackets [].
Example
string myString = "Hello";
cout << myString[0];
// Outputs H
Note: String indexes start with 0: [0] is the first character. [1] is the second
character, etc.
Example
string myString = "Hello";
cout << myString[1];
// Outputs e
Example
string myString = "Hello";
myString[0] = 'J';
cout << myString;
// Outputs Jello instead of Hello
string txt = "We are the so-called "Vikings" from the north.";
The backslash (\) escape character turns special characters into string
characters:
\\ \ Backslash
The sequence \" inserts a double quote in a string:
Example
string txt = "We are the so-called \"Vikings\" from the north.";
Example
string txt = "It\'s alright.";
Example
string txt = "The character \\ is called backslash.";
\n New Line
\t Tab
Example
string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
Example
string fullName;
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName;
From the example above, you would expect the program to print "John Doe",
but it only prints "John".
That's why, when working with strings, we often use the getline() function
to read a line of text. It takes cin as the first parameter, and the string
variable as second:
Example
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;