Count lines in a string with IndexOf

image_pdfimage_print
   
 

public class MainClass{
        public static long CountLinesInString(string text)
        {
            long count = 1;
            int start = 0;
            while ((start = text.IndexOf('
', start)) != -1)
            {
                count++;
                start++;
            }
            return count;
        }
}

   
     


This entry was posted in Data Types. Bookmark the permalink.