Reputation: 83
I have a .txt file, the data of which I have stored in a long string. There are many single new line characters in the string after every line. And there are double new line characters at the end of paragraphs. What I want is to split the string into an array of paragraphs.
what I thought is the following but it is not working
string filePath = "C:\\Users\\Data.txt";
StreamReader readFile = new StreamReader(filePath);
string Data = readFile.ReadToEnd();
string[] paragraphss = Regex.Split(Data, "(^|[^\n])\n{2}(?!\n)");
please help thank you
Upvotes: 1
Views: 5448
Reputation: 1562
Inspired by @LueTm's answer and @Traubenfuchs' comment, just making it look compiler friendly and complete. Here's how to split a string with double new line characters:
Data.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);
Upvotes: -1
Reputation: 1832
On windows systems the newline character is \r\n
, on Unix systems it is \n
. This may be why the lines aren't being split, because you're specifically looking for \n\n
instead of \r\n\r\n
.
You can however use Environment.Newline
, which will return the correct newline character for whatever environment the software is running on.
Upvotes: 0
Reputation: 2380
If you're OK with not using regex, Data.Split("\n\n")
should do the trick.
Upvotes: 1