CSharp examples for System:String Strip
Removes blank lines from the beginning of the string..
using System.Linq; using System.Diagnostics.Contracts; using System;/* ww w . ja v a 2s .co m*/ public class Main{ /// <summary> /// Removes blank lines from the beginning of the string.. /// </summary> /// <param name="text">The text.</param> /// <param name="numberLines">The number lines.</param> /// <returns>A new string with any blank lines at the beginning removed.</returns> public static string RemoveLinesFromBeginning(this string text, int numberLines) { if (string.IsNullOrEmpty(text)) { return string.Empty; } if (text.CountLines() < numberLines) { return string.Empty; } int start = 0; for (int i = 0; i < numberLines; i++) { start = text.IndexOf(Environment.NewLine, start, StringComparison.CurrentCulture); if (start < 0) { return string.Empty; } start += Environment.NewLine.Length; } if (start < text.Length) { return text.Substring(start); } return string.Empty; } /// <summary> /// Counts the number of lines in the string. /// </summary> /// <param name="text">The text.</param> /// <returns></returns> public static int CountLines(this string text) { if (string.IsNullOrEmpty(text)) { return 0; } int count = 0; int start = 0; bool done = false; while (done == false) { int n = text.IndexOf(Environment.NewLine, start, StringComparison.CurrentCulture); if (n == -1) { done = true; if (start < text.Length) { count++; } } else { start = n + Environment.NewLine.Length; count++; } } return count; } }