有没有一种方法可以找到字符串中每个(指定)单词的索引,并且有一种方法可以在找到(指定)单词时运行方法

时间:2020-02-07 18:51:24

标签: c# winforms

我正在尝试找到一种方法来获取字符串中每个指定单词的索引:

输入
表示要查找的单词的字符串

期望的行为:
运行一个方法并给它索引 (有点像foreach

我需要它来找到单词,以便为整个指定的单词上色。

喜欢这个(我在这里使用粗体,但想象它是彩色的)

输入:

string text = "I want to throw my pc off the window. I want to go party" 

string word = "want"

markup(text, word , richtextbox) 

输出:

想要将我的电脑扔出窗户。我想要参加派对


Google没有给出任何有用的结果。

3 个答案:

答案 0 :(得分:0)

正则表达式是您的朋友。 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
        }

您可以使用索引突出显示文本,而不必像本示例中那样打印索引。

答案 1 :(得分:0)

如果您想找到单词索引来对Richtextbox进行一些更改,它应该可以工作:

void Main()
{
   string text = "I want to throw my pc off the window. I want to go party";
   string word = "want";

    var indexes = GetWordIndexes(text,word)
    foreach (var inx in indexes)
    {
        markup(text, inx, richtextbox)
    }
}

public IEnumerable<int> GetWordIndexes(string text, string word)
{
    int wordLength = word.Length;
    for (int i = 0; i < text.Length; i++)
    {
        if (!(text.Length - wordLength < i) && text.Substring(i,wordLength) == word) 
            yield return i;
    }
}

答案 2 :(得分:0)

您可以使用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);
    }
}

有关示例用法,请在表单上放置RichTextBoxTextBoxButton控件,并将此代码添加到Button.Click事件中:

private void button1_Click(object sender, EventArgs e)
{
    HighlightText(richTextBox1, textBox1.Text);
}

输出

enter image description here


更新

如果要一次突出显示多个项目,则可以创建一个方法的重载,该方法采用字符串数组,然后为该数组中的每个项目调用上述方法:

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);
}

输出

enter image description here