All with Contains on lower case

全字母句
全字母句 在 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.

2026年09月23日 · 覺得有幫助嗎?