C++

C++ Multiline String Literals

The most efficient approach to displaying numerous string statements in a structured and efficient manner is with a C++ multiline string. When we have a long string, this can be useful. It’s not good to maintain such text on a single line. It affects the readability of the code.

Many methodologies can be utilized to construct C++ multiline strings, which will be discussed in this article. By enclosing a string in quotes, we can split it across many lines. Brackets can be used to split a string into numerous lines. Furthermore, the backslash character in C++ is used to continue a line. Let’s explore each method in more detail. In addition to the explanations of each method, we have included examples.

Example 1: Using String Literals for Multiline String in C++

If two or more string literals are adjacent, C++ enables implicit string concatenation, which allows the compiler to connect them. In C++, we can use implicit concatenation to make a multiline string literal, as seen below:

We have the multiline string implementation in the following program. The program has included the libraries at the initial stage. The string must be included to access string functionality in the program. There the program’s main function is called. In the main function’s block, we have the standard string class “std::string,” given the name “MultilineString”. The multiline string has three string literals separated into three lines.

The string content is in the double quotes representing that it’s a string. So these string literals are close together, the C++ compiler will concatenate them. The Multiline string literals will be printed on the output string.

#include <iostream>

#include <string>

 

int main()
{
std::string MultilineString =
        "Issac Newton "
        "made his discovery of gravity "
        "after seeing an apple fall from tree.";

std::cout<< "Multiline String:"<<MultilineString<< std::endl;

    return 0;
}

In the following image, the multiline string literals are concatenated together and represented as a single string.

Example2: Using Backslash Character for Multiline String in C++

The compiler eliminates the new line and previous backslash character when using a backslash character at the end of a line. This is how the multiline string is created. In contrast to the prior method, indentation is important here.

Let’s start the program demonstration. First, we have C++ libraries as required for the program. After that, there is a string declaration in the int main function. We have used standard class string representation and defined a string named “BackslashString”. The string literals use the backslash symbol “\” to join the multiline string literals.

Note that every string literal includes a backslash. The space at the beginning of string literal is maintained here in the program. The indentation is very important when using backslash symbols at the end of every string literal. Then, the standard cout class is being called for displaying the multiline string that uses the backslash symbol.

#include <iostream>

#include <string>

 

int main()
{
std::string BackslashString = "Don't stop \
until you are\
proud to yourself."
;

std::cout<<BackslashString<< std::endl;

    return 0;
}

The output of using a backslash at the end of the string literal is printed upon the compilation of the above code.

Example 3: Utilizing Raw String Literals

The raw string literal is the best way to create a multiline string literal. This method is straightforward and efficient; however, it only works with C++. It’s important to note that the string retains all spaces, newlines, and indentations.

The below program has imported the header files as it is an initial step of every C++ program. In the next step, we have the main function for program execution. In the main function’s body, we have called the standard string class “std::string” for declaring the string. The string is declared as “MyString,” and the string contains the raw representation of the multiline string.

We used the “R” keyword for raw string literals, then wrapped the multiline strings literals in the double quotes and passed them into the round brackets. The important thing about raw string literals is that all the whitespaces, newlines of string literals, and indentation are preserved here. The multiline string will be printed after the raw string literal representation.

#include <iostream>

#include <string>

 

int main()
{
std::string MyString = R"(You will face many
defeats in life, but never
let yourself be defeated)."
;

std::cout<<"Multiline String Literals:"<<MyString<< std::endl;

    return 0;
}

As you can see, the multiline string is shown as the output of the above program. Also, the new line and indentation are retained by the raw string literal.

Example 4: Using Macros

Lastly, with C++, we can generate a multiline string using macros. In this case, indentation is irrelevant, and the method substitutes a single space for numerous whitespace characters.

The macro is used to specify any constant value or variable with its value throughout the program that will be substituted by a macro name, where the macro comprises the section of code that will be triggered when the name of the macro is used.

In the header file, we have defined a macro as “MULTILINE_STRING” and passed the variable “s” as the parameter, and used this variable “s” with the hashtag processor. Now we’re in the program’s main function, which has a string name “StringIs” specified in it. The string used the macros name “MULTILINE_STRING,” and we stored the literal string in the macros. The macros representation of multiline string literals will be printed upon the compilation time of the program.

#include <iostream>

#include <string>

 

#define MULTILINE_STRING(s) #s

int main()
{
std::string StringIS = MULTILINE_STRING(Programming are skills
        best acquired by practice
and example rather than
        from books.);

std::cout<< "Multiline String Literals: " <<StringIS<< std::endl;

    return 0;
}

The output of utilizing macros to express multiline strings is displayed in the figure below.

Conclusion

The multiline string representation is mostly used for commenting on something in the code. If we want to use the multiline used as a string in the code and will be executed, then enclose it in the string representation. We have discussed various methodologies for the multiline string used in C++ with the running example programs. Examine each of the approaches; they are simple to comprehend and implement in C++.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content