2011-09-05 33 views
0

之間的內容我很新的VS.和C#。這裏我有Richtextbox。我想過濾兩個詞之間的內容。填充在RichTextBox的兩個詞C#

This is the example way to do my processing. This text is on my Richtextbox

我想在字example和字之間text來填充內容。 答案是way to do my processing. This

請幫我做這個使用C#。

回答

1

你可以使用正則表達式,或者假設text是您的RichTextBox的文本,此代碼:

string from = "example"; 
int iStart = text.IndexOf(from) + from.Length; 
int iEnd = text.IndexOf("text", iStart); 
string result = text.Substring(iStart, iEnd - iStart); 
0

你可以這樣做:

string strTest = 
    "This is the example way to do my processing. This text is on my Richtextbox"; 
strTest = strTest.Substring(strTest.IndexOf("example") + 8); 
strTest = strTest.Substring(0,strTest.IndexOf("text")-1); 
0

我認爲這個問題是讓文本出來在RichTextBox的,請嘗試:

var textRange = new TextRange(
    richTextBox.Document.ContentStart, 
    richTextBox.Document.ContentEnd); 
var text = textRange.Text; 

,然後...

+0

'答案'在這裏以_example_字開頭,並沒有得到所有需要的單詞 – Marco

+0

@Marco:糟糕,當然是。我正在關注如何從RichTextBox中獲取文本的問題。對不起,我的錯。要獲得正確的子串,您的解決方案是正確的方法。 – WaltiD

+0

它發生在我身上yesyerday :) – Marco