Question:
How can I make my form search case insensitive C#?
Toby
2013-04-08 13:32:29 UTC
Currently, I am using the following code:

int index = 0;
string temp = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text;
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text = "";
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text = temp;
while (index < Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text.LastIndexOf(textBox1.Text))
{
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Find (textBox1.Text, index, Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.TextLength, RichTextBoxFinds.None);
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionBackColor = Color.Yellow;
index = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text.IndexOf(textBox1.Text, index) +1;

I would like to make the search case insensitive.

How would this be achieved?
Four answers:
Danez
2013-04-08 14:15:23 UTC
If you are using a regular richTextBox you can use the built-in methods Find, IndexOf and LastIndexOf :



string txtToFind = "checker";

int len = this.richTextBox1.TextLength;

int index = 0;

int lastIndex = this.richTextBox1.Text.LastIndexOf(txtToFind, StringComparison.CurrentCultureIgnoreCase);



while (index < lastIndex)

{

this.richTextBox1.Find(txtToFind, index, len, RichTextBoxFinds.None);

this.richTextBox1.SelectionBackColor = Color.Yellow;

index = this.richTextBox1.Text.IndexOf(txtToFind, index, StringComparison.CurrentCultureIgnoreCase) + 1;

}
David W
2013-04-08 14:11:40 UTC
String.IndexOf provides a parameter to specify case insensitivity...for example:



int iLocation = mystring.IndexOf("whatever",

StringComparison.CurrentCultureIgnoreCase);



Or use .ToUpper/ToLower on both of the strings for the compare (won't modify the original)...for example:



bool bStringFound =

mystring.ToUpper().Contains(searchstring. ToUpper() );
TR1990
2013-04-08 13:41:14 UTC
Typically the easiest way to make any comparison case-insensitive is to simply convert both your input and whatever you want to compare it to to the same case. For example:



string myStr = "Convert This To Lower CASe";

string low = myStr.ToLower() //"convert this to lower case"
anonymous
2016-03-08 08:45:34 UTC
phone book student details inventory management


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...