C# Count lines in a string using Regex

Edit

Last modification : 2011-05-26 10:24:16

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication10
{
    class Program
    {
        public static long CountLinesInStringSlow(string text)
        {
            Regex reg = new Regex("\n", RegexOptions.Multiline);
            MatchCollection mat = reg.Matches(text);
            return mat.Count + 1;
        }

        public static void Main()
        {
            Console.WriteLine(CountLinesInStringSlow("line1\nline2\n"));
            // Display : 3
            Console.ReadKey();
        }
    }
}