Strings are variables that represent a group of letters and they are one of the most common variables in C++, as they are in other languages. If you have studied other languages in the past, you may recognize that there are a basic set of commands that you can do with strings in each language, but each language tend to treat strings slightly differently, causing variations in the syntax.
C++ is no different. Unlike languages like Java, strings are mutable. What this means is that we can change the string value without having to completely overwrite it with a new value. As a result of this, we see many additional commands available to use with strings in C++ compared to a language such as Java. While we will explore some of those commands in this tutorial, others can be found in the C++ reference guide here.
A string literal is any value in the program that is included inside of double quotes. For example "Hello World"
is a string literal. Most of the time, we take these string literals and store them in a variable. To do this, we create a new variable of type string
.
Our most basic string command is to concatenate two strings. We can do this by adding them together. Check out the two examples below. See if you can add a third string into the example.
One of the most common string functions is to take a substring, or part of a string. We do this using the substr
command. Here is an example:
In the example above, we are taking a portion of the original string and assign it to the variable sub
. In this case, we take the portion starting at index 3 until the end of the string. Since C++ uses a zero index, we start our substring just before the number 3, so sub
will be equal to 3456
.
We can also take a portion of the string without taking the remainder. For example, if we just wanted to capture a substring equal to 45
in the above example, we would use the following line:
Notice how unlike other languages, in C++ we specify the starting position and the length of the substring, not the ending position.
Take some time to play around with the example below.
Another common tasks with strings are looping through a string and searching through a string. Let’s take a look at a basic look through a string:
Notice above how we introduce the length commands. This command returns the length of a string. For example, if our string is Hello
, str.length() will return 5. Keep in mind, that the length of the string may be 5, but the indices run from 0 to 4.
Try the example below.
Many times, we may want to know if a command a string or character is present in another string. To do this in C++, we use the find
command, however the use of it is a little more complicated compared to other languages. In many other languages, when we search for a string it returns the index of where it is found, or a -1 if it is not found. In C++, if the index is not found, it doesn’t return a position and we detect this by checking it we are equal to the string npos
value. Check out the example below:
In the program below, the user is prompted to enter a password. Check to see if the password is at least 8 characters long. If not, print out too small.
If it is 8 characters, check to see if it contains the number 5. Print out the position if it does, otherwise, print out that there is no 5.