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

4

91

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).

Franciscofranciska answered 4/10, 2010 at 21:13 Comment(3)
Here is an interesting tidbit: The C++ lexer doesn't actually care about how many quotes you place before and after a string with only two exceptions. The number of quotes you use must be odd and they must match on either side. """"" This is a valid string and will be parsed """"". However, there are no special properties given to these strings, they simply behave like single quotes.Rosa
Interesting thanks for sharing that... is there any useful purpose for that? You might be able to use that as a subtle tag for different groups of strings in a code for external parsing by perl/bash/python scripts. That's about all I can think of for now. :)Franciscofranciska
@ThomasAnthony That just happens because it will treat the quotes around the end as a bunch of empty strings and concatenate them together - it's not a feature, it's standard C/C++ behaviourKeitt
D
155

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.

Dougherty answered 4/10, 2010 at 21:15 Comment(7)
Can you mix in other items like an int constant? If not how do I add these?Franciscofranciska
@Jason: What do you mean by mixing in an int constant?Dougherty
my_function(arg_1, arg_2, "hello world this string is " TWO "lines long!" );Franciscofranciska
with a line break between the TWO and the "lines long"Franciscofranciska
Combining string literals is actual part of the lexical phase (not the pre-processor), as string splitting like this is defined as part of the language.Hypsometer
@Eclipse... this was too long to express clearly in a comment, so I broke it into a separate question... see my latest question, and take a crack at it, if you can.Franciscofranciska
@Jason, from your follow-up question, it looks like you are needing something like the STRINGIFY macro above.Dougherty
F
11

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++.

Ferullo answered 4/10, 2010 at 21:15 Comment(0)
L
8

I don't know if it is an extension in GCC or if it is standard, but it appears you can continue a string literal by ending the line with a backslash (just as most types of lines can be extended in this manor in C++, e.g. a macro spanning multiple lines).

#include <iostream>
#include <string>

int main ()
{
    std::string str = "hello world\
    this seems to work";

    std::cout << str;
    return 0;
}
Lauber answered 4/10, 2010 at 22:38 Comment(3)
That literal syntax contains quite a few blanks between world and thisManhour
@TokenMacGuy: indeed it does, I hadn't noticed that. Easy enough to fix if you move the second (and subsequent) lines all the way to the left. But that would really mess with the appearance of your indenting.Lauber
Yea that was my initial approach, but I abandoned it due to the indentation/spacing issues @Manhour outlines. Good to note though.Franciscofranciska
G
0

I don't have enough rep to comment but I thought it was important to point out that, in meador's answer, the extra spaces between "world" and "this" are caused by the indentation spaces before "this".

Galahad answered 1/5 at 19:22 Comment(1)
you oculd just write the full answerKenyettakenyon

© 2022 - 2024 — McMap. All rights reserved.