Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting C++ Strings Onto Multiple Lines (Code Syntax, Not Parsing)

Not to be confused with how to split a string parsing wise, e.g.:
Split a string in C++?

I am a bit confused as to how to split a string onto multiple lines in c++.

This sounds like a simple question, but take the following example:

#include <iostream> #include <string> main() {   //Gives error   std::string my_val ="Hello world, this is an overly long string to have" +     " on just one line";   std::cout << "My Val is : " << my_val << std::endl;    //Gives error   std::string my_val ="Hello world, this is an overly long string to have" &     " on just one line";     std::cout << "My Val is : " << my_val << std::endl; } 

I realize that I could use the std::string append() method, but I was wondering if there was any shorter/more elegant (e.g. more pythonlike, though obviously triple quotes etc. aren't supported in c++) way to break strings in c++ onto multiple lines for sake of readability.

One place where this would be particularly desirable is when you're passing long string literals to a function (for example a sentence).

like image 298
Jason R. Mick Avatar asked Oct 04 '10 21:10

Jason R. Mick


People also ask

How do you separate a multi line in C language?

"\ operator is used to separate a multiline macro in C language" Explanation: In C language, macros are used to substitute a string, which in turn defined as a macro substitution. In C, macros can be used to replace a first part using a second part.

How do I write a multi line string literal in C?

We can use string literal concatenation. Multiple string literals in a row are joined together: char* my_str = "Here is the first line." "Here is the second line."; But wait!

How do you break a string into two lines?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

How do I write multiple lines of a string in C++?

Using Backslash If we place a backslash at the end of each line, the compiler removes the new-line and preceding backslash character. This forms the multiline string.


2 Answers

Don't put anything between the strings. Part of the C++ lexing stage is to combine adjacent string literals (even over newlines and comments) into a single literal.

#include <iostream> #include <string> main() {   std::string my_val ="Hello world, this is an overly long string to have"      " on just one line";   std::cout << "My Val is : " << my_val << std::endl; } 

Note that if you want a newline in the literal, you will have to add that yourself:

#include <iostream> #include <string> main() {   std::string my_val ="This string gets displayed over\n"      "two lines when sent to cout.";   std::cout << "My Val is : " << my_val << std::endl; } 

If you are wanting to mix a #defined integer constant into the literal, you'll have to use some macros:

#include <iostream> using namespace std;  #define TWO 2 #define XSTRINGIFY(s) #s #define STRINGIFY(s) XSTRINGIFY(s)  int main(int argc, char* argv[]) {     std::cout << "abc"   // Outputs "abc2DEF"         STRINGIFY(TWO)         "DEF" << endl;     std::cout << "abc"   // Outputs "abcTWODEF"         XSTRINGIFY(TWO)          "DEF" << endl; } 

There's some weirdness in there due to the way the stringify processor operator works, so you need two levels of macro to get the actual value of TWO to be made into a string literal.

like image 192
Eclipse Avatar answered Sep 21 '22 23:09

Eclipse


Are they both literals? Separating two string literals with whitespace is the same as concatenation: "abc" "123" is the same as "abc123". This applies to straight C as well as C++.

like image 27
Matt K Avatar answered Sep 24 '22 23:09

Matt K