在rixhtextbox中搜索两个或更多匹配的搜索文本 [英] Searching two or more matched search text in rixhtextbox

查看:82
本文介绍了在rixhtextbox中搜索两个或更多匹配的搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索richtextbox中的文本,但是一次只能找到一个匹配的文本....但是我的文档中有多个搜索到的文本.我可以获得任何帮助吗?

I am trying to search the text in richtextbox but i get only one matched text at a time....but my documents have more than one searched text .can i get any help?

推荐答案

为了给您一个想法,让我们从最简单的任务开始:在整个文本中从上到下查找子字符串的所有出现.

To give you an idea, let''s start with the simplest task: find all occurrences of a sub-string in the whole text from top to bottom.

static int[] FindAllMatches(string source, string pattern) {
    System.Collections.Generic.List<int> list =
        new System.Collections.Generic.List<int>();
    int len = pattern.Length;
    int current = 0;
    while (true) {
        int position = source.IndexOf(pattern, current);
        if (position < 0) break;
        list.Add(position);
        current = position + len;
    }
    return list.ToArray();
} //FindAllMatches

//...
int[] matches = FindAllMatches(myRichTextBox, pattern);



使用这个想法(改变搜索开始的索引),您可以使用其他选项进行搜索:选定的文本或其他子范围,向后搜索.

您没有询问正则表达式搜索.类Regex通过其自己的算法进行搜索,您将需要使用其Matches.这是一个不同的主题.

-SA



Using this idea (shifting the index of search start) you can search with other options: selected text or other sub-range, backward search.

You did not ask about Regular Expression search. The class Regex does the search by its own algorithm, you will need to use its Matches. This is a different topic.

—SA


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.SelectionBackColor = Color.White;
            richTextBox1.DeselectAll();
            String searchkeyword = this.textBox1.Text;
            String richboxtext = richTextBox1.Text;
            int loop = richboxtext.Length;
            int searchlength = searchkeyword.Length;
            for (int i = 0; i < loop; i++)
            {
                if (searchkeyword[0] == richboxtext[i])
                {
                    if (i != 0)
                    {
                        if (searchkeyword.Equals(richboxtext.Substring(i, searchlength)))
                        {
                            richTextBox1.Select(i, searchlength);
                            richTextBox1.SelectionBackColor = Color.Yellow;
                            richTextBox1.DeselectAll();
                            i = i + searchlength;
                        }
                    }
                    else
                    {
                        if (searchkeyword.Equals(richboxtext.Substring(i, searchlength)))
                        {
                            richTextBox1.Select(i, searchlength);
                            richTextBox1.SelectionBackColor = Color.Yellow;
                            richTextBox1.DeselectAll();
                            i = i + searchlength;
                        }
                    }
                }
            }
        }
    }
}


我不知道您尝试了什么,但是我会使用正则表达式.

阅读本文: http://www.regular-expressions.info/dotnet.html [ ^ ]
I don''t know what you''ve tried, but I would use regular expressions.

Read this article: http://www.regular-expressions.info/dotnet.html[^]


这篇关于在rixhtextbox中搜索两个或更多匹配的搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆