0

The real Author name is : PolarBear.

My function searched PolarBear, the result PolarBear was highlighted as I wished.

I searched Polarbear, polarbear and poLarbear, the PolarBear did appeared in result but was not highlighted as I wished.

How can I make the highlighting case insensitive like the search does? Thank you.

Highlighting Code:

    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;
        }
merv
  • 331
  • 5
  • 25

2 Answers2

2

Add .ToLower() when you are setting the keyword and shadow variables. Then the highlighting should work as you expect:

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

Alternatively, you can tell IndexOf to be case insensitive:

index = shadow.IndexOf(keyword, pointer, StringComparison.OrdinalIgnoreCase); 
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Thank you Brian, both way work! :) Ignore my last reply if you saw it. Thank you so much. – merv Aug 21 '12 at 03:06
1

You can try using an overload of the IndexOf method that uses a StringComparison enumeration.

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

from second MSDN link:

When you call a string comparison method such as String.Compare, String.Equals, or String.IndexOf, you should always call an overload that includes a parameter of type StringComparison so that you can specify the type of comparison that the method performs. For more information, see Best Practices for Using Strings in the .NET Framework.

small working test example:

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
  • 53,938
  • 9
  • 94
  • 111
  • Thank you Mark Hall, but in my case `IndexOf` doesn't work i am not sure why. Adding `.ToLower()` does the trick! Thank you for your time teaching me. I appreciate much! :) – merv Aug 21 '12 at 02:22
  • @Shyuan Not sure what you did, tested it out and it works for me. Made a quick little application with a textbox, richtextbox and Button. Will add to answer. – Mark Hall Aug 21 '12 at 02:32
  • Hi Mark, my fault. I misplaced the `index`. I tried. It works now :) Sorry and thanks a bunch! – merv Aug 21 '12 at 03:04
  • But what's the difference between `OrdinalIgnoreCase` and `InvariantCultureIgnoreCase`? Sorry I am very new to programming. – merv Aug 21 '12 at 03:05
  • @Shyuan Take a look a Sky Sanders and Guffa's Answers in this [SO question](http://stackoverflow.com/questions/2749662/string-comparison-invariantcultureignorecase-vs-ordinalignorecase). – Mark Hall Aug 21 '12 at 03:09
  • I am looking at exactly what you show me and I was about to tell you that I found the explanation. :) Thanks a lot. :) – merv Aug 21 '12 at 03:20