//Constructing string...StringBuilder sb = new StringBuilder();sb.AppendLine("first line");sb.AppendLine("second line");sb.AppendLine("third line");string s = sb.ToString();Console.WriteLine(s);
//Splitting multiline string into separate linesstring[] splitted = s.Split(new string[] {System.Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
// Output (separate lines)for( int i = 0; i < splitted.Count(); i++ ){Console.WriteLine("{0}: {1}", i, splitted[i]);}
var s = "Hello\r\nWorld";var path = Path.GetTempFileName();using (var writer = new StreamWriter(path)){writer.Write(s);}var lines = File.ReadLines(path);
Private Shared Function SplitLines(text As String) As String()Return text.Split({Environment.NewLine, vbCrLf, vbLf}, StringSplitOptions.None)End Function
using System.IO;
string textToSplit;
if (textToSplit != null){List<string> lines = new List<string>();using (StringReader reader = new StringReader(textToSplit)){for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()){lines.Add(line);}}}
string[] result;
// Pass a string, and the delimiterresult = string.Split("My simple string", " ");
// Split an existing string by delimiter onlystring foo = "my - string - i - want - split";result = foo.Split("-");
// You can even pass the split options parameter. When omitted it is// set to StringSplitOptions.Noneresult = foo.Split("-", StringSplitOptions.RemoveEmptyEntries);
string Splitstring(string txt, int n = 120, string AddBefore = "", string AddAfterExtra = ""){//Spit each string into a n-line length list of stringsvar Lines = Enumerable.Range(0, txt.Length / n).Select(i => txt.Substring(i * n, n)).ToList();
//Check if there are any characters left after split, if so add the restif(txt.Length > ((txt.Length / n)*n) )Lines.Add(txt.Substring((txt.Length/n)*n));
//Create return text, with extrasstring txtReturn = "";foreach (string Line in Lines)txtReturn += AddBefore + Line + AddAfterExtra + Environment.NewLine;return txtReturn;}