0

真实作者姓名是:PolarBear。

我的功能 searched PolarBear,结果PolarBear如我所愿突出显示。

我搜索了Polarbearpolarbear并且确实出现在结果中poLarbearPolarBear但没有如我所愿突出显示。

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

高亮代码:

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

2 回答 2

2

.ToLower()在设置keywordshadow变量时添加。然后突出显示应该按您的预期工作:

string keyword = txtComAuthor.Text.ToLower();
string shadow = richComByTemplate.Text.ToLower();

或者,您可以告诉IndexOf不区分大小写:

index = shadow.IndexOf(keyword, pointer, StringComparison.OrdinalIgnoreCase); 
于 2012-08-21T02:02:58.267 回答
1

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

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

    }
}
于 2012-08-21T02:04:10.310 回答