如何测试字符串是否仅由不同的字符组成?

-1 c# string character

我有一个5个字符长的字符串Console.Readline(),我想测试所有5个字符是否不同.

Sel*_*enç 6

您可以使用Distinct将为您提供不同字符的方法,然后将计数与输入进行比较length,如果它们相等则表示所有字符都不同.

string input = Console.ReadLine();

bool isDifferent = input.Distinct().Count() == input.Length;
Run Code Online (Sandbox Code Playgroud)

注意你需要using System.Linq;使用Distinct方法.