【问题标题】:c# if String Contains 2 "hallo" [duplicate]c#如果字符串包含2个“你好”[重复]
【发布时间】:2011-12-24 14:31:04
【问题描述】:

可能重复:
How would you count occurences of a string within a string (C#)?

我想检查一个字符串是否包含两个东西..

String hello = "hellohelloaklsdhas";

if hello.Contains(*hello 2 Times*); -> True

我该如何解决这个问题?

【问题讨论】:

  • 你想要 2 个,还是至少 2 个?

标签: c# string


【解决方案1】:
public static class StringExtensions
{
    public static int Matches(this string text, string pattern)
    {
        int count = 0, i = 0;
        while ((i = text.IndexOf(pattern, i)) != -1)
        {
            i += pattern.Length;
            count++;
        }
        return count;
    }
}

class Program
{
    static void Main()
    {
        string s1 = "Sam's first name is Sam.";
        string s2 = "Dot Net Perls is about Dot Net";
        string s3 = "No duplicates here";
        string s4 = "aaaa";

        Console.WriteLine(s1.Matches("Sam"));  // 2
        Console.WriteLine(s1.Matches("cool")); // 0
        Console.WriteLine(s2.Matches("Dot"));  // 2
        Console.WriteLine(s2.Matches("Net"));  // 2
        Console.WriteLine(s3.Matches("here")); // 1
        Console.WriteLine(s3.Matches(" "));    // 2
        Console.WriteLine(s4.Matches("aa"));   // 2
    }
}

【讨论】:

    【解决方案2】:

    索引:

    int FirstIndex = str.IndexOf("hello");
    int SecondIndex = str.IndexOf("hello", FirstIndex + 1);
    if(FirstIndex != -1 && SecondIndex != -1)
    {
      //contains 2 or more hello
    }
    else
    {
       //contains not
    }
    

    或者如果你想要 2:if(FirstIndex != -1 && SecondIndex != -1 && str.IndexOf("hello", SecondIndex) == -1)

    【讨论】:

      【解决方案3】:

      索引

      可以使用IndexOf方法获取某个字符串的索引。这个方法有一个重载,它接受一个起点,从哪里看。当没有找到指定的字符串时,返回-1

      这是一个不言而喻的例子。

      var theString = "hello hello bye hello";
      int index = -1;
      int helloCount = 0;
      
      while((index = theString.IndexOf("hello", index+1)) != -1)
      {
          helloCount++;
      }
      
      return helloCount==2;
      

      正则表达式

      获取计数的另一种方法是使用正则表达式:

      return (Regex.Matches(hello, "hello").Count == 2);
      

      【讨论】:

        【解决方案4】:

        如果您使用正则表达式 MatchCollection,您可以轻松获得:

        MatchCollection matches;
        
        Regex reg = new Regex("hello"); 
        
        matches = reg.Matches("hellohelloaklsdhas");
        return (matches.Count == 2);
        

        【讨论】:

        • 为什么需要matchposition
        • @Vlad:你没有。这是复制/粘贴的保留。谢谢。
        • 或者你可以只使用一个衬里静态方法。
        • 有无数种方法可以完成这项任务。我个人最喜欢@m0skit0 的回答。我很想指出您的第一个答案使用 .Match 而不是 .Matches 但我认为您最终会理解它:)
        【解决方案5】:

        你可以使用正则表达式:)

        return Regex.Matches(hello, "hello").Count == 2;
        

        这与模式 "hello" 的字符串 hello 匹配,如果计数为 2,则返回 true。

        【讨论】:

        • 在我的情况下这不起作用,因为我正在寻找的字符串是一个点(“。”)。所以我把它改成“\”而不是“你好”。它运作良好。谢谢。
        【解决方案6】:

        正则表达式。

        if (Regex.IsMatch(hello,@"(.*hello.*){2,}"))
        

        我猜你的意思是“hello”,这将匹配一个至少有 2 个“hello”(不完全是 2 个“hello”)的字符串

        【讨论】:

        • +1 用于匹配计数正则表达式 :-)
        • 谢谢解答,明天试试
        【解决方案7】:

        new Regex("hello.*hello").IsMatch(hello)

        Regex.IsMatch(hello, "hello.*hello")

        【讨论】:

          【解决方案8】:

          您可以使用正则表达式,并检查 Matches 函数结果的长度。如果是两个你就赢了。

          【讨论】:

            猜你喜欢
            • 2017-06-13
            • 1970-01-01
            • 2011-05-14
            • 2014-02-19
            • 1970-01-01
            • 2018-12-20
            • 2017-10-11
            • 2011-08-31
            • 2020-04-07
            相关资源
            最近更新 更多