How to Split the Definition of a Long String Over Multiple Lines

How do I split the definition of a long string over multiple lines?

Are you talking about multi-line strings? Easy, use triple quotes to start and end them.

s = """ this is a very
long string if I had the
energy to type more and more ..."""

You can use single quotes too (3 of them of course at start and end) and treat the resulting string s just like any other string.

NOTE: Just as with any string, anything between the starting and ending quotes becomes part of the string, so this example has a leading blank (as pointed out by @root45). This string will also contain both blanks and newlines.

I.e.,:

' this is a very\n        long string if I had the\n        energy to type more and more ...'

Finally, one can also construct long lines in Python like this:

 s = ("this is a very"
"long string too"
"for sure ..."
)

which will not include any extra blanks or newlines (this is a deliberate example showing what the effect of skipping blanks will result in):

'this is a verylong string toofor sure ...'

No commas required, simply place the strings to be joined together into a pair of parenthesis and be sure to account for any needed blanks and newlines.

How to split a string literal across multiple lines in C / Objective-C?

There are two ways to split strings over multiple lines:

  1. Each string on its own line. Works only with strings:

    • Plain C:

      char *my_string = "Line 1 "
      "Line 2";
    • Objective-C:

      NSString *my_string = @"Line1 "
      "Line2"; // the second @ is optional
  2. Using \ - can be used for any expression:

    • Plain C:

      char *my_string = "Line 1 \
      Line 2";
    • Objective-C:

      NSString *my_string = @"Line1 \
      Line2";

The first approach is better, because there isn't a lot of whitespace included. For a SQL query however, both are possible.

NOTE: With a #define, you have to add an extra \ to concatenate the two strings:

Plain C:

#define kMyString "Line 1"\
"Line 2"

Bash: split long string argument to multiple lines?

You can assign your string to a variable like this:

long_arg="my very long string\
which does not fit\
on the screen"

Then just use the variable:

mycommand "$long_arg"

Within double quotes, a newline preceded by a backslash is removed. Note that all the other white space in the string is significant, i.e. it will be present in the variable.

How can I split a long string in source code into several lines for more readability in C++?

Simply remove the +'s:

#include <string>

int main()
{
std::string menu = "MENU:\n"
"1. option 1\n"
"2. option 2\n"
"3. option 3\n"
"4. 10 more options\n";
}

Adjacent string literals are automatically concatenated by the compiler.

Alternatively, in C++11, you can use raw string literals, which preserve all indentation and newlines:

#include <string>

int main()
{
std::string menu = R"(MENU:
1. option 1
2. option 2
3. option 3
4. 10 more options
)";

}

How to split long strings over multiple lines in yaml without introducing whitespace characters

You have omitted the double-quoted scalar style, which is exactly what you need:

example:
string1: "Oneverylongunbrokenlinethatmustnotcon\
tainanyresultingwhitespaceintheoutput"

A double-quoted scalar is the only scalar style in YAML that allows being broken into multiple lines at any position. This is done by escaping the linebreak. Would you not escape the linebreak, you would get a space just like with most of the other scalar styles.

This does not work with any other style because only double-quoted scalars process escape sequences.

Swift - Split string over multiple lines

Swift 4 includes support for multi-line string literals. In addition to newlines they can also contain unescaped quotes.

var text = """
This is some text
over multiple lines
"""

Older versions of Swift don't allow you to have a single literal over multiple lines but you can add literals together over multiple lines:

var text = "This is some text\n"
+ "over multiple lines\n"

#define string over multiple lines

Just get rid of the whitespace in between the lines, and quote the whole thing. A \ at the EOL basically "escapes" the newline, so it won't be part of the string itself. It's only relevant for the preprocessor:

#include <stdio.h>

#define LONG_STRING "How to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple Linesaa\
bbbb\
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"

int main ( void ) {
printf(LONG_STRING);
return 0;
}

That works just fine

For aesthetic reasons, you can quote each line separately, the only requirement is you add the \ directly after the closing quotes:

#define LONG_STRING "How to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple LinesHow to Split the Definition of a Long String Over Multiple Linesaa"\
"bbbbbb"\
"ccccccccccccccccccccccccccccccccccccccccccccc"

This, too, works just fine

Note:

The two suggestions are not 100% equivalent. The first version defines a macro to be a single string literal. The second version defines the macro as 3 separate string literals. For the most part, this isn't a big deal, because during the translation phase, adjacent string literal tokens should be concatenated:

5.1.1.2 Translation phases:

[...]

6. Adjacent string literal tokens are concatenated.

7. White-space characters separating tokens are no longer significant. Each
preprocessing token is converted into a token. The resulting tokens are
syntactically and semantically analyzed and translated as a translation unit.

I could not find the footnote Meninx mentions about C99 behaving differently. Document I used can be found here



Related Topics



Leave a reply



Submit