Frederik Today

Calculating Article Reading Time in .Net

Letting your readers know how much time it takes to read your content helps improve user experience.
Letting your readers know how much time it takes to read your content helps improve user experience. I saw many articles by people, that use string.split to count the words. The problem I have with string.split is that it will create (immutable) strings for every word, and that is heavy on the garbage collector, certainly when you have a long-form article.

Count Words (up to .net6)

public static int CountWords(this string str)
{
return Regex.Matches(str, @"[\w-]+").Count();
}

Count Words (.net7)

public static int CountWords(this string str)
{
return Regex.Count(str, @"[\w-]+");
}

Calculate the reading Time

Most people can read between 200 and 250 words per minute The easiest way to calculate is to divide the number of words by the speed.
public static int CountMinutesRead(this string str)
{
return Convert.ToInt32(str.CountWords() / 228);
}
These functions will be added to the CodeHelper.Core.Extensions packages soon!