c# 在文本中查找某物或某物

c# find in text something or something

我需要在文本中搜索,但是 richtextbox.Find("something"); 中是否可以有多个选项?例如richtextbox.Find("something" or "somethingelse");

你可以实现这样的东西(扩展方法 FindAny for RichTextBox):

  public static class RichTextBoxExtensions {
    public static int FindAny(this RichTextBox source, params String[] toFind) {
      if (null == source)
        throw new ArgumentNullException("source");
      else if (null == toFind)
        throw new ArgumentNullException("toFind");

      int result = -1;

      foreach (var item in toFind) {
        if (null == item)
          continue;

        int v = source.Find(item);

        if ((v >= 0) && ((result < 0) || (v < result)))
          result = v;
      }

      return result;
    }
  }

....

  int result = richtextbox.FindAny("something", "somethingelse");