How to Split String Across New Lines and Keep Blank Lines

How to split string across new lines and keep blank lines?

I'd recommend using lines instead of split for this task. lines will retain the trailing line-break, which allows you to see the desired empty-line. Use chomp to clean up:

"aaaa\nbbbb\n\n".lines.map(&:chomp)
[
[0] "aaaa",
[1] "bbbb",
[2] ""
]

Other, more convoluted, ways of getting there are:

"aaaa\nbbbb\n\n".split(/(\n)/).each_slice(2).map{ |ary| ary.join.chomp }
[
[0] "aaaa",
[1] "bbbb",
[2] ""
]

It's taking advantage of using a capture-group in split, which returns the split text with the intervening text being split upon. each_slice then groups the elements into two-element sub-arrays. map gets each two-element sub-array, does the join followed by the chomp.

Or:

"aaaa\nbbbb\n\n".split(/(\n)/).delete_if{ |e| e == "\n" }
[
[0] "aaaa",
[1] "bbbb",
[2] ""
]

Here's what split is returning:

"aaaa\nbbbb\n\n".split(/(\n)/)
[
[0] "aaaa",
[1] "\n",
[2] "bbbb",
[3] "\n",
[4] "",
[5] "\n"
]

We don't see that used very often, but it can be useful.

How to split string with empty new line

Escape  Description            ASCII-Value
\n New Line Feed (LF) 10
\r Carriage Return (CR) 13

So you need to try string.split("\n\r") in your case.

Edit

If you want to split by empty line, try \n\r\n\r. Or you can use .readLine() to read your file, and skip all empty lines.

Are you sure it's 10 13 10 13? It always should be 13 10...

And, you should not depend on line.separator too much. Because if you are processing some files from *nix platform, it's \n, vice versa. And even on Windows, some editors use \n as the new line character. So I suggest you to use some high level methods or use string.replaceAll("\r\n", "\n") to normalize your input.

How do I split a string on an empty line using .Split()?

You can get it accomplished by using

string[] people = record.Split(new string[] { "\r\n\r\n" },
StringSplitOptions.RemoveEmptyEntries);

or

string[] people = record.Split(new string[] { Environment.NewLine + Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries);

What it does is it removes empty entries with StringSplitOptions.RemoveEmptyEntries and then splits where two linebreaks are right after each other.

Split text file into Strings on empty line

you can split a string to an array by

String.split();

if you want it by new lines it will be

String.split("\\n\\n");

UPDATE*

If I understand what you are saying then john.

then your code will essentially be

BufferedReader in
= new BufferedReader(new FileReader("foo.txt"));

List<String> allStrings = new ArrayList<String>();
String str ="";
while(true)
{
String tmp = in.readLine();
if(tmp.isEmpty())
{
if(!str.isEmpty())
{
allStrings.add(str);
}
str= "";
}
else if(tmp==null)
{
break;
}
else
{
if(str.isEmpty())
{
str = tmp;
}
else
{
str += "\\n" + tmp;
}
}
}

Might be what you are trying to parse.

Where allStrings is a list of all of your strings.

Easiest way to split a string on newlines in .NET?

To split on a string you need to use the overload that takes an array of strings:

string[] lines = theText.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None
);

Edit:

If you want to handle different types of line breaks in a text, you can use the ability to match more than one string. This will correctly split on either type of line break, and preserve empty lines and spacing in the text:

string[] lines = theText.Split(
new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);

Best way to split string into lines

  • If it looks ugly, just remove the unnecessary ToCharArray call.

  • If you want to split by either \n or \r, you've got two options:

    • Use an array literal – but this will give you empty lines for Windows-style line endings \r\n:

      var result = text.Split(new [] { '\r', '\n' });
    • Use a regular expression, as indicated by Bart:

      var result = Regex.Split(text, "\r\n|\r|\n");
  • If you want to preserve empty lines, why do you explicitly tell C# to throw them away? (StringSplitOptions parameter) – use StringSplitOptions.None instead.

How to split while keeping the empty line?

Use re.split

re.split(r'[ \t]', s)

This would do splitting on white spaces or tabs.

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.



Related Topics



Leave a reply



Submit