C # how to separate by paragraph?
so I know that the paragraph is char 10 + char 13 I:
streamreader sr = new streamreader();
string s = sr.ReadToEnd();
string s1 = s.Replace((char)10, "*");
string s2 = s1.Replace((char)13, "*");
Now it changed the paragraphs to two **, but how did I split into 2 characters? Does anyone have alternatives to paragraph splitting?
- a way to easily break paragraphs OR
- splitting method with two characters
+3
source to share
5 answers
string doc = "line1\r\nline2\r\nline3";
var docLines = doc.Split(new string[] { "\r\n" }, System.StringSplitOptions.None);
Alternatively, you can use Environment.NewLine ... which would keep the standard.
var docLines = doc.Split(new string[] { Environment.NewLine }, System.StringSplitOptions.None);
+4
source to share
See string.Split (string [], StringSplitOption) :
var result = s2.Split(new []{"**"}, StringSplitOption.RemoveEmptyEntries)
Also you can do it using Environment.NewLine without converting it to **:
var result = s.Split(new []{Enviornment.NewLine}, StringSplitOption.RemoveEmptyEntries)
+1
source to share
Have you tried Regex
? Windows uses \r
(13), denoted \n
(10) as the line separator, so you get lines. But if you want blocks of text separated by at least one blank line, you can try this:
string inputString = sr.ReadToEnd();
string[] paragraphs = Regex.Split(inputString , "(\r\n){2,}");
+1
source to share