Search code examples
c#textfindrichtextbox

c# find in text something or something


I need to search in text, but is it possible to have more than one option in richtextbox.Find("something");? For example richtextbox.Find("something" or "somethingelse");


Solution

  • You can implement something like this (an extension method 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");