How to Split a Multi-Line String into Multiple Lines

How do I split a multi-line string into multiple lines?

inputString.splitlines()

Will give you a list with each item, the splitlines() method is designed to split each line into a list element.

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 do I split a multi-line string into multiple lines in python?

you could try to find 2 lines (with lookahead inside it to avoid capturing the linefeed) or only one (to process the last, odd line). I expanded your example to show that it works for more than 3 lines (with a little "cheat": adding a newline in the end to handle all cases:

import re

s = "Line 1\nLine 2\nLine 3\nline4\nline5"
result = re.findall(r'(.+?\n.+?(?=\n)|.+)', s+"\n")

print(result)

result:

['Line 1\nLine 2', 'Line 3\nline4', 'line5']

the "add newline cheat" allows to process that properly:

    s = "Line 1\nLine 2\nLine 3\nline4\nline5\nline6"

result:

['Line 1\nLine 2', 'Line 3\nline4', 'line5\nline6']

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.

Bash splitting a multi line string by a multi-character delimiter into an array

In case you don't want to change default RS value then could you please try following.

awk '{gsub("DELIMITER",ORS)} 1' Input_file

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"

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"


Related Topics



Leave a reply



Submit