-3

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

Ilona
  • 357
  • 3
  • 10
  • 7
    What *exactly* do you mean by "empty entries"? What happens with the code you've got? Can you give sample input, actual output and expected output? – Jon Skeet Mar 09 '15 at 13:54
  • With `empty entries`, you mean `white space`? – Soner Gönül Mar 09 '15 at 13:54
  • @John Skeet yes I already edited my question. – heroeofTheDesert Mar 09 '15 at 13:55
  • 1
    Are you trying to split a string at a new line or only when there is two new lines? – Sayse Mar 09 '15 at 13:56
  • you want to split the string by "\n"? – Greg Oks Mar 09 '15 at 13:56
  • Unfortunately, newlines in strings are not always the same. .NET will usually use the character sequence `"\r\n"`, but depending on where the string came from, it could instead use `"\n"` or even `"\r"`. That said, if you know for sure the character sequence in use in the string you can use it repeated twice in a row as your split string E.g. `"\r\n\r\n"` – Peter Duniho Mar 09 '15 at 13:58
  • @GregOks When I'm splitting the string by "\n" I will get three strings but I just want to split by the empty line between "old." and "I" – heroeofTheDesert Mar 09 '15 at 13:58
  • depends on how have you stored all these words, is it string. or string[] or string[][] – Muds Mar 09 '15 at 14:00
  • 1
    @heroeofTheDesert: to repeat the first comment: _"Can you give sample input, actual output and expected output? "_ You have now accomplished the first task. – Tim Schmelter Mar 09 '15 at 14:12
  • @TimSchmelter sorry for taking this much time. I think he edited his comment? I didn't remember this part. – heroeofTheDesert Mar 09 '15 at 14:25
  • @heroeofTheDesert: now you have provided an example. But unfortunately it's still not clear. You want a `string[]` as result? But you want to keep some empty lines, others should be removed. What is the rule, why do you want to keep that after `ACT I` and why do you want to remove it after `SCENE I. An open place.`? What is the resulting `string[]`. Show a desired output like Ministry has done [in his answer](http://stackoverflow.com/a/28944166/284240). – Tim Schmelter Mar 09 '15 at 14:42
  • Yes I want a string[] as result. I don't want to keep empty lines. One moment. – heroeofTheDesert Mar 09 '15 at 14:47
  • @heroeofTheDesert: in what way is [Ministry's answer](http://stackoverflow.com/a/28944166/284240) incorrect? – Tim Schmelter Mar 09 '15 at 14:57
  • @TimSchmelter it worked out for me now. Thank you for taking the time and keep helping. I'm sorry for asking such a inaccurate question. Have a nice day! – heroeofTheDesert Mar 09 '15 at 14:59

2 Answers2

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

Ministry
  • 116
  • 7
  • @heroeofTheDesert All good. Please mark this as the accepted answer if it matches your criteria. Additionally, good luck on /r/dailyprogrammer ;) – Ministry Mar 09 '15 at 15:02
  • nice to see that someone copying my answer and make som uppercaseletters to it is getting accepted while i get 5 downvotes... i think to improve my answer to the correct code should be possible to the OP... – swe Mar 09 '15 at 15:35
  • The problem wasn't limited to the character casing. Additionally the Environment.NewLine didn't work using the macbeth.text. – Ministry Mar 09 '15 at 15:42
-5

if you are searching for empty lines you should do somthing like

"value".split(environment.newline+environment.newline)
swe
  • 1,416
  • 16
  • 26