In our previous tutorial we discussed C++ Variables and Data Types, today, we are going to introduce a new data type, the string class.
C++ String
The string class represents what you may know as a string of text. Remember our previous C++ Hello World program? Well, you can also think of the output to the terminal as a string of text.
Below is some simple usage of the string class:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; int main() { string message; message = "Hello World!"; cout << message << endl; return 0; } |
The output of course, would be a simple “Hello World!” to the console ending with a newline.
String class functions
Until now, we haven’t discussed much about functions. We’ll discuss them in more detail later, but for now just know that functions provide us more functionality with a data type. We’ll only be discussing functions that either modify or access more information about a data type.
For example, think of it as a black box. You provide it an object, lets say a car, and you provide some input such as color of the car, type of engine you want, and other options. Well, this function (our black box) would return a car modified to your inputs.
The beauty of functions, is that you don’t need to know what happens internally! You simply provide the object and inputs if necessary, and you should receive your desired output. We’ll discuss later how to create our own functions.
In order to call a function, you simply specify the identifier , function name, and input parameters if necessary. Follow the examples provided below and you’ll understand. We’ll be continuing the previous usage example, so feel free to keep adding to your program.
For the following functions, take into consideration how the string is stored. This will help you in understanding the following functions.
length()
| length | Returns the size of a string in characters |
1 2 3 4 5 | // Stores the size of our message in an int value int size = message.length(); // The result will be 12. A space is counted as a character. cout << size << endl; |
substr()
| substr | Generates a substring based on two input parameters |
1 2 3 4 5 | // Generates a substring of size 5, starting at position 2 string substring = message.substr(2,5); // The result is "llo W" cout << substring << endl; |
operator+
| + | Concatenate two strings together |
1 2 3 4 5 6 7 | string a = "Hello "; string b = "World!"; string c = a + b; // Will display "Hello World!"; cout << c << endl; |
The best way to learn how to use strings, is by using them.
Going further with the String class
Now that you understand the basics of strings why not do the following?
- Develop a program that contains your first and last name in two respective variables called first and last.
Concatenate your first and last name to create another variable, fullname.
Now, using the substr function, separate both your first and last name from the fullname variable.
For example this should be the output:
1 2 3 4 5 6
First name(first): Diego Last name(last): Villasenor Full name(fullname): Diego Villasenor First using substr(fullname): Diego Last using substr(fullname): Villasenor

