C# 有没有办法设置不区分大小写,以便突出显示richtextbox中有大写和小写的单词?

C# 有没有办法设置不区分大小写,以便突出显示richtextbox中有大写和小写的单词?,c#,winforms,C#,Winforms,真正的作者名字是:PolarBear 我的函数搜索了PolarBear,结果PolarBear如我所愿突出显示 我搜索了Polarbear,Polarbear和Polarbear,结果中确实出现了Polarbear,但没有如我所愿突出显示 如何使突出显示像搜索一样不区分大小写?多谢各位 突出显示代码: private void searchComByAuthor() { // Process the list of files found in the direc

真正的作者名字是:PolarBear

我的函数搜索了
PolarBear
,结果
PolarBear
如我所愿突出显示

我搜索了
Polarbear
Polarbear
Polarbear
,结果中确实出现了
Polarbear
,但没有如我所愿突出显示

如何使突出显示像搜索一样不区分大小写?多谢各位

突出显示代码:

    private void searchComByAuthor()
    {
        // Process the list of files found in the directory. 
        string[] fileEntries = Directory.GetFiles(sourceDirXML);
        foreach (string fileName in fileEntries)
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

            string docPath = fileName;

            xmlDoc.Load(docPath); //* load the XML document from the specified file.

            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

            foreach (XmlNode node in nodeList)
            {

                XmlElement itemElement = (XmlElement)node;

                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;

                if (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower())
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                    string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                    richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
                                                "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
                }
            }
        }

        int pointer = 0;
        int index = 0;
        string keyword = txtComAuthor.Text;
        string shadow = richComByTemplate.Text;

        while (true)
        {
            //Searching in the copy/shadow
            index = shadow.IndexOf(keyword, pointer);
            //if keyword not found then the loop will break
            if ((index == -1) || (String.IsNullOrEmpty(keyword)))
            {
                break;
            }
            richComByTemplate.Select(index, keyword.Length);
            richComByTemplate.SelectionColor = Color.Red;
            richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
            pointer = index + keyword.Length;
        }
设置
关键字
阴影
变量时,添加
.ToLower()
。然后,突出显示应按预期工作:

string keyword = txtComAuthor.Text.ToLower();
string shadow = richComByTemplate.Text.ToLower();
或者,您可以告诉
IndexOf
不区分大小写:

index = shadow.IndexOf(keyword, pointer, StringComparison.OrdinalIgnoreCase); 
设置
关键字
阴影
变量时,添加
.ToLower()
。然后,突出显示应按预期工作:

string keyword = txtComAuthor.Text.ToLower();
string shadow = richComByTemplate.Text.ToLower();
或者,您可以告诉
IndexOf
不区分大小写:

index = shadow.IndexOf(keyword, pointer, StringComparison.OrdinalIgnoreCase); 

您可以尝试使用使用枚举的方法的重载

index = shadow.IndexOf(keyword, pointer,StringComparison.InvariantCultureIgnoreCase); 
从第二个MSDN链接:

调用字符串比较方法(如string.Compare、string.Equals或string.IndexOf)时,应始终调用包含StringComparison类型参数的重载,以便指定该方法执行的比较类型。有关更多信息,请参阅在.NET Framework中使用字符串的最佳实践

小型工作测试示例:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int pointer = 0;
        int index = 0;
        string keyword = txtComAuthor.Text;
        string shadow = richComByTemplate.Text;

        while (true)
        {
            //Searching in the copy/shadow 
            index = shadow.IndexOf(keyword, pointer, StringComparison.InvariantCultureIgnoreCase);
            //if keyword not found then the loop will break 
            if ((index == -1) || (String.IsNullOrEmpty(keyword)))
            {
                break;
            }
            richComByTemplate.Select(index, keyword.Length);
            richComByTemplate.SelectionColor = Color.Red;
            richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
            pointer = index + keyword.Length;
        } 

    }
}

您可以尝试使用使用枚举的方法的重载

index = shadow.IndexOf(keyword, pointer,StringComparison.InvariantCultureIgnoreCase); 
从第二个MSDN链接:

调用字符串比较方法(如string.Compare、string.Equals或string.IndexOf)时,应始终调用包含StringComparison类型参数的重载,以便指定该方法执行的比较类型。有关更多信息,请参阅在.NET Framework中使用字符串的最佳实践

小型工作测试示例:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int pointer = 0;
        int index = 0;
        string keyword = txtComAuthor.Text;
        string shadow = richComByTemplate.Text;

        while (true)
        {
            //Searching in the copy/shadow 
            index = shadow.IndexOf(keyword, pointer, StringComparison.InvariantCultureIgnoreCase);
            //if keyword not found then the loop will break 
            if ((index == -1) || (String.IsNullOrEmpty(keyword)))
            {
                break;
            }
            richComByTemplate.Select(index, keyword.Length);
            richComByTemplate.SelectionColor = Color.Red;
            richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
            pointer = index + keyword.Length;
        } 

    }
}

谢谢Mark Hall,但就我而言,
IndexOf
不起作用,我不知道为什么。添加
.ToLower()
就可以了!谢谢你花时间教我。我非常感激!:)@Shyuan不知道你做了什么,测试了一下,对我有用。用textbox、richtextbox和按钮制作了一个快速的小应用程序。会增加答案的。嗨,马克,我的错。我把
索引放错地方了。我试过了。它现在可以工作了:)对不起,非常感谢!但是,
OrdinalIgnoreCase
InvariantCultureIgnoreCase
之间有什么区别呢?对不起,我对编程很陌生。@Shyuan看看Sky Sanders和Guffa在这方面的答案。谢谢Mark Hall,但就我而言,
IndexOf
不起作用,我不知道为什么。添加
.ToLower()
就可以了!谢谢你花时间教我。我非常感激!:)@Shyuan不知道你做了什么,测试了一下,对我有用。用textbox、richtextbox和按钮制作了一个快速的小应用程序。会增加答案的。嗨,马克,我的错。我把
索引放错地方了。我试过了。它现在可以工作了:)对不起,非常感谢!但是,
OrdinalIgnoreCase
InvariantCultureIgnoreCase
之间有什么区别呢?对不起,我对编程很陌生。@Shyuan看看Sky Sanders和Guffa在这方面的回答。谢谢你,Brian,双向工作!:)如果你看到了,请忽略我最后的回复。非常感谢。谢谢你,布莱恩,双向工作!:)如果你看到了,请忽略我最后的回复。非常感谢你。