Ignore case strings

shah123's Avatar
Contributor
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,
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Use CompareNoCase
or convert both of them to Lower/Upper case before using the contains.
shah123's Avatar
Contributor
How to put CompareNoCase in this method?
shah123's Avatar
Contributor
Any help?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
shah123's Avatar
Contributor
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?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
shah123's Avatar
Contributor
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.