【问题标题】:how to check if string has more than two repeating characters如何检查字符串是否有两个以上的重复字符
【发布时间】:2021-05-02 01:02:20
【问题描述】:

我正在尝试检查一个字符串是否包含两个以上的重复字符。

例如

'aabcd123' = ok
'aaabcd123' = not ok
'aabbab11!@' = ok
'aabbbac123!' = not ok

我尝试过这样的事情,但没有运气

if (string.Distinct().Count() > 2){ 
                    //do something
                }

任何帮助将不胜感激。

【问题讨论】:

  • “没有运气”是什么意思? if 语句返回什么?您是否考虑过使用 Regular Expression 来完成此操作?
  • Also...
  • 为什么'aaabcd123' 不行?它确实包含两个以上的重复字符。
  • Distinct() 返回一个包含原始集合中不同元素的集合,而 Count() 只是计算它。这就是为什么你对那句话没有运气。
  • 我认为您需要更清楚地了解您要查找的内容。在我看来,您正在寻找原始字符串中相邻重复字符的子字符串不超过 2 个字符的字符串。那是对的吗? (我选择这个是因为它与您的输入样本相匹配……但从您的描述中看不出这就是您所追求的。)

标签: c# string duplicates


【解决方案1】:

这个对我有用:

public bool  IsOK(string s)
{
  if(s.Length < 3) return true;

  return !s.Where((c,i)=> i >= 2 && s[i-1] == c && s[i-2] == c).Any();
}

'aabcd123'     : OK
'aaabcd123'    : not OK
'aabbab11!@'   : OK
'aabbbac123!'  : not OK

【讨论】:

    【解决方案2】:

    只是为了经典循环:

    public bool HasRepeatingChars(string str)
    {
        for(int i = 0; i < str.Length - 2; i++)
            if(str[i] == str[i+1] && str[i] == str[i+2])
                return true;
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      您可以为此使用正则表达式:

      return Regex.IsMatch(inputString, @"(.)\1{2,}", RegexOptions.IgnoreCase)
      

      这会检查任何字符,然后检查至少 2 次相同的字符。甚至可以使用诸如“AaA”之类的字符串,并且可以进行修改以准确找出这些字符是什么,它们在字符串中出现的位置等等(还允许您将这些子字符串替换为其他内容)

      更多信息:

      http://msdn.microsoft.com/en-us/library/6f7hht7k.aspx

      http://msdn.microsoft.com/en-us/library/az24scfc.aspx

      【讨论】:

        【解决方案4】:

        由于您要查找字符串中的三个字符而不仅仅是三个字符,因此您需要循环并查看三个连续的字符以查看它们是否相同。这样的循环会起作用。

        string myString = "aaabbcd";
        bool hasMoreThanTwoRepeatingCharsInARow = false;
        for(int index = 2; index < myString.Length; index++)
        {
          if(myString[index] == myString[index - 1] && myString[index] == myString[index - 2])
          {
            hasMoreThanTwoRepeatingCharsInARow = true;
          }
        }
        

        我会坚持这个方法,让变量更好,你很高兴!

        【讨论】:

          【解决方案5】:
              [TestMethod]
              public void Test()
              {
                  const string sample1 = "aabcd123";
                  const string sample2 = "aaabcd123";
                  const string sample3 = "aabbab11!@";
                  const string sample4 = "aabbbac123!";
          
                  Assert.IsTrue(IsOk(sample1));
                  Assert.IsFalse(IsOk(sample2));
                  Assert.IsTrue(IsOk(sample3));
                  Assert.IsFalse(IsOk(sample4));
              }
          
              private bool IsOk(string str)
              {
                  char? last = null;
                  var i = 1;
                  foreach (var c in str)
                  {
                      if (last == c)
                      {
                          i++;
                          if (i > 2) return false;
                      }
                      else
                      {
                          i = 1;
                      }
                      last = c;
                  }
                  return true;
              }
          

          【讨论】:

            【解决方案6】:

            使用正则表达式

            (?:.*<letter>.*){2}
            

            例如,对于字母 b,您会使用

            (?:.*b.*){2}
            

            对于所有元音

             (?:.*[aeiou].*){2}
            

            有关 c# 上的正则表达式用法,请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=net-5.0

            【讨论】:

              猜你喜欢
              • 2023-03-31
              • 2020-07-16
              • 2017-02-01
              • 2021-01-11
              • 2012-04-21
              • 1970-01-01
              • 1970-01-01
              • 2013-09-04
              • 1970-01-01
              相关资源
              最近更新 更多