C# 是否有一种方法可以找到字符串中每个(指定)单词的索引,并在找到(指定)单词时运行方法
我试图找到一种方法来获取字符串中每个指定单词的索引: 输入:C# 是否有一种方法可以找到字符串中每个(指定)单词的索引,并在找到(指定)单词时运行方法,c#,winforms,C#,Winforms,我试图找到一种方法来获取字符串中每个指定单词的索引: 输入: 表示要查找的单词的字符串 期望的行为: 运行一个方法并为其提供索引 (类似于foreach的foreach) 我需要它来找到这个词,这样我就可以把指定的词涂上颜色 像这样(我在这里使用粗体,但想象它是彩色的) 输入: string text = "I want to throw my pc off the window. I want to go party" string word = "want" markup(text,
表示要查找的单词的字符串 期望的行为:
运行一个方法并为其提供索引 (类似于foreach的
foreach
)
我需要它来找到这个词,这样我就可以把指定的词涂上颜色
像这样(我在这里使用粗体,但想象它是彩色的)
输入:
string text = "I want to throw my pc off the window. I want to go party"
string word = "want"
markup(text, word , richtextbox)
输出:
我想把我的电脑扔出窗外。我想去参加聚会
谷歌没有给出任何有用的结果。正则表达式是你的朋友
IndexOf
仅获取子字符串的第一个实例,但我们需要所有子字符串
string text = "I want to throw my pc off the window. I want to go to a party.";
string word = "want";
string pattern = $@"({word})";
MatchCollection matches = Regex.Matches(text, pattern);
foreach(Match match in matches)
{
Console.WriteLine(match.Index); // print indexes
}
您可以使用索引突出显示文本,而不是像本例中那样打印索引 如果您想找到word索引以对richtextbox进行一些更改,那么它应该可以工作:
void Main()
{
string text=“我想把我的电脑从窗口扔出去。我想去参加聚会”;
字符串word=“want”;
var index=GetWordIndexes(文本,单词)
foreach(索引中的var inx)
{
标记(文本、inx、richtextbox)
}
}
公共IEnumerable GetWordIndexes(字符串文本、字符串字)
{
int wordLength=word.Length;
for(int i=0;i
您可以使用IndexOf
方法获取一个字符串在另一个字符串中的索引。它使用一个参数来指定从何处开始查找,因此您可以在循环中不断调用它,并在每次迭代中增加起始索引。如果返回-1
,则表示未找到字符串,因此我们可以将其用作循环的条件
例如,此方法接受一个RichTextBox
控件和一个string
进行搜索,并将突出显示RTB文本中搜索文本的所有实例:
private static void HighlightText(RichTextBox rtb, string text, Color? highlight = null)
{
if (rtb == null || rtb.TextLength == 0 || text == null || text.Length == 0) return;
// Find the first index of the text
var index = rtb.Text.IndexOf(text);
var length = text.Length;
var color = highlight ?? Color.Red; // Use Red if no color was specified
// While we found a match
while (index > -1)
{
// Highlight it
rtb.SelectionStart = index;
rtb.SelectionLength = length;
rtb.SelectionColor = color;
// Then try to find the next index of the text (starting after the previous one)
index = rtb.Text.IndexOf(text, index + length);
}
}
对于示例用法,请在表单上放置RichTextBox
、TextBox
和按钮
控件,并将此代码添加到按钮。单击事件:
private void button1_Click(object sender, EventArgs e)
{
HighlightText(richTextBox1, textBox1.Text);
}
输出
更新
如果要一次高亮显示多个项,可以创建方法重载,该重载接受字符串数组,然后为数组中的每个项调用上述方法:
private static void HighlightItems(RichTextBox rtb, string[] items, Color? highlight = null)
{
if (items == null || items.Length == 0) return;
foreach (var item in items)
{
HighlightText(rtb, item, highlight);
}
}
调用此函数的一种示例方法是将textbox1
中的文本拆分为分号字符。然后我们可以将结果数组传递给上面的重载方法:
private void button1_Click(object sender, EventArgs e)
{
// Un-highlight the text first
richTextBox1.SelectAll();
richTextBox1.SelectionColor = Color.Black;
// Call highlight with an array of strings created by
// splitting textbox1.Text on the ';' character
var multipleItems = textBox1.Text.Split(';');
HighlightItems(richTextBox1, multipleItems);
}
输出
语言是C#→ 对,你有一个C标记,不需要把它作为标题。你的意思是谷歌根本没有提到IndexOf
方法?你正在寻找字符串。替换?IndexOf
只返回子字符串的第一个实例。他应该改用Regex.Matches
。。查看注释并检查其中一个测试方法中的警告。是否有方法修改代码段以接受多个标记,或者我应该多次调用它?(抱歉,如果这是一个愚蠢的问题,我对UI有点陌生)当然,您可以编写另一个具有类似签名的方法,该方法接受一个字符串数组,然后在该方法中为数组中的每个项调用另一个方法。我已经寄了一个样品。