2011-09-05 22 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