Diese Methode fügt dem übergeben String Zeilennummern hinzu. Ideal z.B. für Quellcodedarstellung in einer RichTextBox.
/// <summary>
/// Adds the line numbers to text.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
private string AddLineNumbersToText(string text)
{
StringBuilder sb = new StringBuilder();
string[] splitStrings = {"\r\n", "\r", "\n"};
string[] lines = text.Split(splitStrings, StringSplitOptions.None);
int lineCounter = 0;
foreach (string s in lines)
{
sb.Append(lineCounter.ToString());
sb.Append(":\t");
sb.Append(s);
sb.Append(Environment.NewLine);
lineCounter++;
}
return sb.ToString();
}
2 Kommentare zum Snippet