Ignore case strings

Discussion in 'C#' started by shah123, Apr 10, 2007.

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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,
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,341
    Likes Received:
    379
    Trophy Points:
    83
    Use CompareNoCase
    or convert both of them to Lower/Upper case before using the contains.
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    How to put CompareNoCase in this method?
     
  4. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,341
    Likes Received:
    379
    Trophy Points:
    83
    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.
     
  6. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,341
    Likes Received:
    379
    Trophy Points:
    83
    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.
     
  8. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice