13. Introduction to Strings – Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards

© Slobodan Dmitrović 2020
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6047-0_13

13. Introduction to Strings

Slobodan Dmitrović1 
(1)
Belgrade, Serbia
 
Earlier, we mentioned printing out a string literal such as "Hello World ." to standard output via:
std::cout << "Hello World.";

We can store these literals inside std::string type. C++ standard library offers a compound type called string or rather std::string as it is part of the std namespace. We use it for storing and manipulating strings.

13.1 Defining a String

To use the std::string type, we need to include the <string> header in our program:
#include <string>
int main()
{
    std::string s = "Hello World.";
}
To print out this string on the standard output we use:
#include <iostream>
#include <string>
int main()
{
    std::string s = "Hello World.";
    std::cout << s;
}

13.2 Concatenating Strings

We can add a string literal to our string using the compound operator += :
#include <iostream>
#include <string>
int main()
{
    std::string s = "Hello ";
    s += "World.";
    std::cout << s;
}
We can add a character to our string using the += operator:
#include <iostream>
#include <string>
int main()
{
    std::string s = "Hello";
    char c = '!';
    s += c;
    std::cout << s;
}
We can add another string to our string using the + operator. We say we concatenate the strings:
#include <iostream>
#include <string>
int main()
{
    std::string s1 = "Hello ";
    std::string s2 = "World.";
    std::string s3 = s1 + s2;
    std::cout << s3;
}

Type string is the so-called class-template . It is implemented using templates, which we will discuss later on. For now, we will just mention that this string class offers some functionality (member functions) for working with strings.

13.3 Accessing Characters

Individual characters of a string can be accessed through a subscript operator [] or via a member function .at(index). The index starts at 0. Example:
#include <iostream>
#include <string>
int main()
{
    std::string s = "Hello World.";
    char c1 = s[0];        // 'H'
    char c2 = s.at(0);     // 'H';
    char c3 = s[6];        // 'W'
    char c4 = s.at(6);     // 'W';
    std::cout << "First character: " << c1 << ", sixth character: " << c3;
}

13.4 Comparing Strings

A string can be compared to string literals and other strings using the equality == operator. Comparing a string to a string literal:
#include <iostream>
#include <string>
int main()
{
    std::string s1 = "Hello";
    if (s1 == "Hello")
    {
        std::cout << "The string is equal to \"Hello\"";
    }
}
Comparing a string to another string is done using the equality operator ==:
#include <iostream>
#include <string>
int main()
{
    std::string s1 = "Hello";
    std::string s2 = "World.";
    if (s1 == s2)
    {
        std::cout << "The strings are equal.";
    }
    else
    {
        std::cout << "The strings are not equal.";
    }
}

13.5 String Input

Preferred way of accepting a string from the standard input is via the std::getline function which takes std::cin and our string as parameters:
#include <iostream>
#include <string>
int main()
{
    std::string s;
    std::cout << "Please enter a string: ";
    std::getline(std::cin, s);
    std::cout << "You entered: " << s;
}

We use the std::getline because our string can contain white spaces. And if we used the std::cin function alone, it would accept only a part of the string.

The std::getline function has the following signature: std::getline(read_from, into); The function reads a line of text from the standard input (std::cin) into a string (s) variable.

A rule of thumb: if we need to use the std::string type, include the <string> header explicitly.

13.6 A Pointer to a String

A string has a member function .c_str() which returns a pointer to its first element. It is also said it returns a pointer to a null-terminated character array our string is made of:
#include <iostream>
#include <string>
int main()
{
    std::string s = "Hello World.";
    std::cout << s.c_str();
}

This member function is of type const char* and is useful when we want to pass our std::string variable to a function accepting a const char* parameter.

13.7 Substrings

To create a substring from a string, we use the .substr() member function. The function returns a substring that starts at a certain position in the main string and is of a certain length. The signature of the function is: .substring(starting_position, length). Example:
#include <iostream>
#include <string>
int main()
{
    std::string s = "Hello World.";
    std::string mysubstring = s.substr(6, 5);
    std::cout << "The substring value is: " << mysubstring;
}

In this example, we have the main string that holds the value of “Hello World.” Then we create a substring that only has the “World” value. The substring starts from the sixth character of the main string, and its length is five characters.

13.8 Finding a Substring

To find a substring in a string, we use the .find() member function. It searches for the substring in a string. If the substring is found, the function returns the position of the first found substring. This position is the position of a character where the substring starts in the main string. If the substring is not found, the function returns a value that is std::string::npos. The function itself is of type std::string::size_type.

To find a substring “Hello” inside the “This is a Hello World string” string, we write:
#include <iostream>
#include <string>
int main()
{
    std::string s = "This is a Hello World string.";
    std::string stringtofind = "Hello";
    std::string::size_type found = s.find(stringtofind);
    if (found != std::string::npos)
    {
        std::cout << "Substring found at position: " << found;
    }
    else
    {
        std::cout << "The substring is not found.";
    }
}

Here we have the main string and a substring we want to find. We supply the substring to the .find() function as an argument. We store the function’s return value to a variable found. Then we check the value of this variable. If the value is not equal to std::string::npos, the substring was found. We print the message and the position of a character in the main string, where our substring was found