How do I Split a string by empty lines?

heroeofTheDesert

I want to split my string just by empty entries. I know you can remove these with the string options but in my situation I need them to split the string. Thank you!

string[] text = content.Split(' '); 

For example you have this string:

Hi my Name is Max.
I'm 24 years old.

I like programming.

Split the string between old and I.

My Code:

            WebClient client = new WebClient();
            Stream stream = client.OpenRead("https://gist.githubusercontent.com/Quackmatic/f8deb2b64dd07ea0985d/raw/macbeth.txt");
            StreamReader reader = new StreamReader(stream);
            string[] text = reader.ReadToEnd().Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(text[i]);
            }
            Console.ReadLine();

The string is splitted by every new line. The problem is I want to have full paragraphs of my text.

My Output:

string[0] = ACT I.
string[1] = SCENE I. An open place.
string[2] =[An open place. Thunder and lightning. Enter three Witches.]

What I want.

string[0] = ACT I.
string[1] = SCENE I. An open place.
            [An open place. Thunder and lightning. Enter three Witches.]
string[2] = FIRST WITCH.
            When shall we three meet again
            In thunder, lightning, or in rain?

or...

    Doubtful it stood;
    As two spent swimmers that do cling together
    And choke their art. The merciless Macdonwald, 
    Worthy to be a rebel, for to that
    The multiplying villainies of nature
    Do swarm upon him, from the Western isles
    Of kerns and gallowglasses is supplied;
    And fortune, on his damned quarrel smiling,
    Show'd like a rebel's whore. But all's too weak;
    For brave Macbeth, well he deserves that name, 
    Disdaining fortune, with his brandish'd steel,
    Which smok'd with bloody execution,
    Like valour's minion,
    Carv'd out his passage, till he fac'd the slave;
    And ne'er shook hands, nor bade farewell to him,
    Till he unseam'd him from the nave to the chaps,
    And fix'd his head upon our battlements

I thought I could get the result by splitting the text by empty lines. .

Ministry
var foo = bar.Split(new string[] {Environment.NewLine + Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

It's important to have the combined Environment.NewLine + Environment.NewLine as this will only split when a new line is also followed by a new line (aka a blank line) and split accordingly

The result is:

foo[0] = "Hi my Name is Max.\r\nI'm 24 years old."
foo[1] = "I like programming."

Edit -- Based on your new input try give this a go:

var web = new WebClient();
var stream = web.OpenRead("https://gist.githubusercontent.com/Quackmatic/f8deb2b64dd07ea0985d/raw/macbeth.txt");
if (stream == null) return;
var reader = new StreamReader(stream).ReadToEnd();
var io = reader.Split(new string[] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries);

Result

From here you can iterate through and do what you please (ie. line[i].StartsWith("ACT") to put it into an object if you so wish)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related