2016-03-28 37 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;',我已經編輯了答案。 –