Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm running the following code within a program and it is not performing comparison the way I want it to, as I'm performing string comparison in Kannada(kn-IN). So I want to perform string comparison using currentculture method to check if it does the job for me. I tried reading it up but I'm not able to understand how to implement it in this code. please mention the references I have to add with your answer.

C#
private void selectWords(string fullText, string searchText)
        {
            string[] words = fullText.Split(' ');
            ArrayList str = new ArrayList();
            foreach (var word in words)
            {
                if (word.Contains(searchText))
                {
                    richTextBox1.Find(word, 0, richTextBox1.TextLength, RichTextBoxFinds.None);
                    richTextBox1.SelectionBackColor = Color.Green;
                    str.Add(richTextBox1.SelectedText);
                }
            }
Posted
Comments
BillWoodruff 24-Sep-14 12:01pm    
Have you tried setting the current culture of the thread:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("kn-IN");

and testing to see if that makes any difference in the behavior of RichTextBox.Find ?

What is source of the string in 'fullText when the method is called ?

What is the relationship of the Text in the RichTextBox to the string in 'fullText ?

Have you done any experimenting with culture-specific string comparison ?
Tejas Shastri 24-Sep-14 12:24pm    
I have not tried anything with the culture specific comparison because I didn't know how to handle the threading.
Does adding that statement to the code affect the comparison? isn't there a need to change that way the comparison is carried out?
Fulltext is the content of the rtb.
BillWoodruff 25-Sep-14 3:50am    
Try setting the CurrentCulture as shown in the code above, and then Richard's code below to overload 'Contains, or use his code to define your own custom string function if you don't want to use an Extension.

1 solution

The Contains method uses an ordinal comparison:
C#
public bool Contains(string value)
{
    return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}

Unfortunately, there isn't an overload of the Contains method which accepts a StringComparison option.

Fortunately, it's easy to write your own:
C#
public static class StringExtensions
{
    public static bool Contains(this string source, string value, StringComparison comparisonType)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.IndexOf(value, comparisonType) >= 0;
    }
}


Then you just need to call the extension method passing in a suitable StringComparison option:
C#
private void selectWords(string fullText, string searchText)
{
    string[] words = fullText.Split(' ');
    ArrayList str = new ArrayList();
    foreach (var word in words)
    {
        if (word.Contains(searchText, StringComparison.CurrentCulture))
        // Or: StringExtensions.Contains(word, searchText, StringComparison.CurrentCulture)
        {
            richTextBox1.Find(word, 0, richTextBox1.TextLength, RichTextBoxFinds.None);
            richTextBox1.SelectionBackColor = Color.Green;
            str.Add(richTextBox1.SelectedText);
        }
    }
}
 
Share this answer
 
Comments
Tejas Shastri 24-Sep-14 23:57pm    
Error 1 Extension method must be defined in a top level static class; StringExtensions is a nested class
Richard Deeming 25-Sep-14 8:15am    
As the error says, the StringExtensions class cannot be nested inside another class.

Either move it out of the parent class, or remove the this keyword before the first parameter and call the method using the static method syntax, as shown in the comment in my answer.
BillWoodruff 25-Sep-14 3:51am    
+5 Excellent advanced lesson here ! I've advised the OP they will need to set the CurrentCulture before using this code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900