My question has two parts:
我的问题有两个部分:
1) How can I search for a sentence (e.g., Dell Canada
) in a string (e.g., I am working in Dell Canada
, and I found it...) .
1)如何在字符串中搜索句子(例如戴尔加拿大)(例如,我在戴尔加拿大工作,我发现它......)。
2)The second part is my string is text in a RichTextBox, so I would like to find the TextRange of that selected sentence and apply certain decoration.
2)第二部分是我的字符串是RichTextBox中的文本,所以我想找到所选句子的TextRange并应用某些装饰。
thanks.
4 个解决方案
#1
The first part is pretty simple as CK has pointed out. Rich text formatting is dictated by certain predefined codes as defined in the RTF specification. First get the underlying RTF raw string from the control using the RTF property
第一部分非常简单,正如CK指出的那样。富文本格式由RTF规范中定义的某些预定义代码决定。首先使用RTF属性从控件获取基础RTF原始字符串
string rawString = richTextBox.Rtf;
string rawString = richTextBox.Rtf;
For eg: the rtf for the phrase 'hello Bobby' will look like this. It is something like HTML, you have tags which define the formatting.
例如:短语'hello Bobby'的rtf将如下所示。它类似HTML,你有标签定义格式。
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 hello Bobby\\par\r\n\\par\r\n}\r\n"
Now suppose I want to make the phrase bold, I would set the Rtf property by replacing the string with
现在假设我想要将短语设为粗体,我将通过替换字符串来设置Rtf属性
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17
\\b hello Bobby\\par\r\n\\par\r\n}\r\n"
Note the \\b before the phrase. That's the code to make a given text bold.
注意短语之前的\\ b。这是使给定文本变粗的代码。
To perform this formatting using code, find the string you want to format(using the first technique) and insert the rtf code in the required position. Hope this helps.
要使用代码执行此格式化,请找到要格式化的字符串(使用第一种技术)并将rtf代码插入所需位置。希望这可以帮助。
For the codes refer MSDN http://msdn.microsoft.com/en-us/library/aa140277.aspx
有关代码,请参阅MSDN http://msdn.microsoft.com/en-us/library/aa140277.aspx
PS: Jeff's version is the easy one. This verison gives you infinite control. If you can do something in WordPad, you can do the same using rtf codes.
PS:Jeff的版本很简单。这个版本为您提供无限的控制。如果您可以在写字板中执行某些操作,则可以使用rtf代码执行相同的操作。
#2
Give this a whirl, it will set it to bold. There are many Selection... properties on the RichTextBox that you can use, also note that it is a case insensitive search:
给它一个旋转,它将它设置为粗体。您可以使用RichTextBox上有许多Selection ...属性,还要注意它是一个不区分大小写的搜索:
string textToSearchFor = "Dell Canada";
int index = richTextBox1.Text.IndexOf(textToSearchFor, StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
richTextBox1.Select(index, textToSearchFor.Length);
richTextBox1.SelectionFont = new Font("Arial", 12f, FontStyle.Bold);
}
else
{
// not found
}
#3
Part 1:
if (myString.IndexOf("Dell Canada") > -1)
{
// do something great;
}
#4
Part 1.
bool cntns = "I am working in Dell Canada, and found it ...".Contains("Dell Canada")
#1
The first part is pretty simple as CK has pointed out. Rich text formatting is dictated by certain predefined codes as defined in the RTF specification. First get the underlying RTF raw string from the control using the RTF property
第一部分非常简单,正如CK指出的那样。富文本格式由RTF规范中定义的某些预定义代码决定。首先使用RTF属性从控件获取基础RTF原始字符串
string rawString = richTextBox.Rtf;
string rawString = richTextBox.Rtf;
For eg: the rtf for the phrase 'hello Bobby' will look like this. It is something like HTML, you have tags which define the formatting.
例如:短语'hello Bobby'的rtf将如下所示。它类似HTML,你有标签定义格式。
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 hello Bobby\\par\r\n\\par\r\n}\r\n"
Now suppose I want to make the phrase bold, I would set the Rtf property by replacing the string with
现在假设我想要将短语设为粗体,我将通过替换字符串来设置Rtf属性
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17
\\b hello Bobby\\par\r\n\\par\r\n}\r\n"
Note the \\b before the phrase. That's the code to make a given text bold.
注意短语之前的\\ b。这是使给定文本变粗的代码。
To perform this formatting using code, find the string you want to format(using the first technique) and insert the rtf code in the required position. Hope this helps.
要使用代码执行此格式化,请找到要格式化的字符串(使用第一种技术)并将rtf代码插入所需位置。希望这可以帮助。
For the codes refer MSDN http://msdn.microsoft.com/en-us/library/aa140277.aspx
有关代码,请参阅MSDN http://msdn.microsoft.com/en-us/library/aa140277.aspx
PS: Jeff's version is the easy one. This verison gives you infinite control. If you can do something in WordPad, you can do the same using rtf codes.
PS:Jeff的版本很简单。这个版本为您提供无限的控制。如果您可以在写字板中执行某些操作,则可以使用rtf代码执行相同的操作。
#2
Give this a whirl, it will set it to bold. There are many Selection... properties on the RichTextBox that you can use, also note that it is a case insensitive search:
给它一个旋转,它将它设置为粗体。您可以使用RichTextBox上有许多Selection ...属性,还要注意它是一个不区分大小写的搜索:
string textToSearchFor = "Dell Canada";
int index = richTextBox1.Text.IndexOf(textToSearchFor, StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
richTextBox1.Select(index, textToSearchFor.Length);
richTextBox1.SelectionFont = new Font("Arial", 12f, FontStyle.Bold);
}
else
{
// not found
}
#3
Part 1:
if (myString.IndexOf("Dell Canada") > -1)
{
// do something great;
}
#4
Part 1.
bool cntns = "I am working in Dell Canada, and found it ...".Contains("Dell Canada")