Declare Multiline String in C++

  1. Use the std::string Class to Declare Multiline String in C++
  2. Use the const char * Notation to Declare Multiline String Literal
  3. Use the const char * Notation With Backlash Characters Declare Multiline String Literal

This article will explain several methods of how to declare a multiline string in C++.

Use the std::string Class to Declare Multiline String in C++

The std::string object can be initialized with a string value. In this case, we declare the s1 string variable as a local variable to the main function. C++ allows multiple double-quoted string literals to be concatenated automatically in a statement. As a result, one might include any number of lines while initializing the string variable and keep the code more consistently readable.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cout; using std::vector;
using std::endl; using std::string;
using std::copy;

int main(){
    string s1 = "This string will be printed as the"
                " one. You can include as many lines"
                "as you wish. They will be concatenated";

    copy(s1.begin(), s1.end(),
         std::ostream_iterator<char>(cout, ""));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

This string will be printed as the one. You can include as many linesas you wish. They will be concatenated

Use the const char * Notation to Declare Multiline String Literal

In most situations, though, it may be more practical to declare a read-only string literal with a const qualifier. This is most practical when the relatively long texts should be outputted to the console, and these texts are mostly static with little or no changes over time. Note that, const qualifier character string needs to be converted to the std::string object before passing as a copy algorithm argument.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::copy;

int main(){
    const char *s2 =
            "This string will be printed as the"
            " one. You can include as many lines"
            "as you wish. They will be concatenated";

    string s1(s2);

    copy(s1.begin(), s1.end(),
         std::ostream_iterator<char>(cout, ""));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

This string will be printed as the one. You can include as many linesas you wish. They will be concatenated

Use the const char * Notation With Backlash Characters Declare Multiline String Literal

Alternatively, one can also utilize the backslash character \ to construct a multiline string literal and assign it to the const qualified char pointer. Shortly, the backslash character needs to be included at the end of each line break, meaning that the string continues on the next line.

Be aware, though, spacing becomes more error-prone to handle as any invisible characters like tabs or spaces will be included in the output. On the other hand, one might utilize this feature to display some patterns more easily to console.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::copy;

int main(){
    const char *s3 = "          This string will\n\
        printed as the pyramid\n\
    as one single string literal form\n";

    cout << s1 << endl;

    printf("%s\n", s3);

    return EXIT_SUCCESS;
}

Output:

          This string will
        printed as the pyramid
    as one single string literal form

Related Article - C++ String

  • Trim a String in C++
  • Parse String Using a Delimiter in C++