C# Line Count String Methods

You have a string and you need to count the number of text lines in it. There are several ways of doing this, such as with regular expressions or IndexOf, but you want to find the simplest and fastest method. Here we look at two excellent solutions for when you need to count the number of lines in a text file using the C# programming language.

=== Benchmark results for line counting (C#) ===

Version A - Count:  188 ms [faster]
Version B - Regex: 5959 ms

Counting lines in string

First, we will look at the string-based method. Also, we see the regular expression based method, which requires the System.Text.RegularExpressions namespace. The methods both have the same result. You will want to place the methods in a separate class, such as a LineCount class in LineCount.cs.

=== Program that counts lines (C#) ===

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        long a = CountLinesInString("This is an\r\nawesome website.");
        Console.WriteLine(a);

        long b = CountLinesInStringSlow("This is an awesome\r\nwebsite.\r\nYeah.");
        Console.WriteLine(b);
    }

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

    static long CountLinesInStringSlow(string s)
    {
        Regex r = new Regex("\n", RegexOptions.Multiline);
        MatchCollection mc = r.Matches(s);
        return mc.Count + 1;
    }
}

=== Output of the program ===

2
3

Description of the two methods. We see two methods above, both of which normally function the same way. The first method uses IndexOf. This finds all the newline characters in the string. The second method uses MatchCollection. This method uses the Matches method with RegexOptions.Multiline. This enumeration value allows newlines to be matched.

See IndexOf String Examples.

Benchmarking the methods

Here we compare the performance of these methods. The author benchmarked the above methods for 1 million operations on real-world files to see just how different they perform. The results were interesting and the numbers are more than one order of magnitude different. Please see the top of this document for results; based on .NET 3.5 SP1.

Summary

Here we looked at how you can count the number of lines in string values using the C# language. We also saw a benchmark of the performance of the Regex-based method and the string-based method that uses IndexOf; generally there is no advantage to the regular expression library when doing simple operations such as this.

See Line Count File Method.

See String Overview.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen