Ignore case strings

Contributor
10Apr2007,19:36   #1
shah123's Avatar
when enter the text to find the string i want to ignore the case how to implement that in a context below
Code:
 public override void Search()
        {
            
            //file = new FileStream(@"C:\username.txt", FileMode.Open, FileAccess.Read);
            
            Console.WriteLine("Input a string ");
            String data = Console.ReadLine();
            sr = File.OpenText(@"C:\username.txt");
            String st = sr.ReadToEnd();
           // How to implement ignore cases 
            
                if (st.Contains(data))
                {
                    Console.WriteLine("Found at position :" + st.IndexOf(data));
                }
                else
                {
                    Console.WriteLine("Not Found");
                }
Thanks,
Go4Expert Founder
10Apr2007,19:39   #2
shabbir's Avatar
Use CompareNoCase
or convert both of them to Lower/Upper case before using the contains.
Contributor
10Apr2007,19:45   #3
shah123's Avatar
How to put CompareNoCase in this method?
Contributor
10Apr2007,20:36   #4
shah123's Avatar
Any help?
Go4Expert Founder
10Apr2007,21:43   #5
shabbir's Avatar
I made an error CompareNoCase is for CString of C++ and not for C# but its CompareTo and I guess the best option is to make each of them to the same case.
Contributor
10Apr2007,21:54   #6
shah123's Avatar
Thanks Shabbir for this but i got one solution on my own but now as Search functionality works i have something same in switch cases


Code:
String s;
                s = Console.ReadLine();
                
                
                switch (s)
                {
                    case "g":
                        Console.Clear();
                        uname.getUserName();
                        return choices();
                       // break;
if i want to enter "G" it doesnt accept how to do that?
Go4Expert Founder
11Apr2007,09:17   #7
shabbir's Avatar
2 Options.
Have a case Fall Through with "G" and g like the one below.
Code:
String s;
s = Console.ReadLine();
switch (s)
{
	case "g":
	case "G":
		Console.Clear();
		uname.getUserName();
		return choices();
	   // break;
or convert the s to the lower case with ToLower.
Contributor
12Apr2007,14:22   #8
shah123's Avatar
Thanks a million, Shabbir. It worked for me. Now i will continue on how to delete a line from the text file. I will do separate thread at some stage.