2016-03-28 58 views
0

我需要在文本中搜索,但是有可能在richtextbox.Find("something");有多个选项吗?例如richtextbox.Find("something" or "somethingelse");c#在文本中找到东西

+0

字符串。载有(“文本的一部分”)的伎俩 – SnakeFoot

回答

0

您可以实现这样的事情(的扩展方法FindAnyRichTextBox):

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"); 
+0

工作。谢谢。但是底部的第三行必须是'return v;',而不是'return = v;' – Stepik

+0

@Stepik:是的,你说得很对;对错字感到抱歉。它应该是'result = v;',我已经编辑了答案。 –