All with Contains on lower case

Pangram
Pangram in C#
using System.Linq;

public static class Pangram
{
    private const string Letters = "abcdefghijklmnopqrstuvwxyz";
    
    public static bool IsPangram(string input)
    {
        var lowerCaseInput = input.ToLower();
        return Letters.All(letter => lowerCaseInput.Contains(letter));
    }
}
  • This begins by lowercasing the input by using ToLower.
  • It then checks if all letters in the alphabet are contained in the input, using the LINQ method All with the String method Contains.

Instead of lowercasing the input, Contains could take an optional StringComparison argument, but a case-insensitive comparison is about seven times slower than lowercasing the input and then doing an exact comparison.

22nd May 2024 · Found it useful?