How to count the lines in a string with C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            long count = CountLinesInString("c#\rc\nc++\r\njava");

            Console.WriteLine(count);
        }
        static long CountLinesInString(string s)
        {
            long count = 1;
            int i = 0;
            while ((i = s.IndexOf('\n', i)) != -1)
            {
                count++;
                i++;
            }
            return count;
        }
    }
}


/*
run:
 
3

*/

 



answered Mar 11, 2017 by avibootz

Related questions

...