【问题标题】:How to check if a string is made of repeating of the same characters?如何检查字符串是否由重复相同的字符组成?
【发布时间】:2011-12-05 17:05:16
【问题描述】:

我正在为我的程序登录。我试图阻止用户输入一串重复字符,例如:111111aaaaaa

我该怎么做?

【问题讨论】:

标签: c# .net validation


【解决方案1】:
string str = ...
bool isValid = str.Distinct().Count() > 1;

【讨论】:

  • 我需要为此添加任何命名空间吗?它不起作用:'txtPassword.Text.Distinct().Count() > 1'。
  • 添加 System.Linq 命名空间。
  • 是的,您需要 System.Linq 命名空间。
【解决方案2】:
string input = ...
bool notAllSame = input.Distinct().Skip(1).Any();

【讨论】:

    【解决方案3】:

    此功能会告诉您是否有重复。它根据原始长度检查不同字符的数量。如果它们不同,则说明您有重复...

    bool containsDups = "ABCDEA".Length != s.Distinct().Count();
    

    问候, 斯蒂芬

    编辑

    在这里找到你的答案: Testing for repeated characters in a string

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2016-02-01
      相关资源
      最近更新 更多