Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. string::find() function for case insensitive search

string::find() function for case insensitive search

Scheduled Pinned Locked Moved C / C++ / MFC
helpalgorithmslearning
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hrishiS
    wrote on last edited by
    #1

    Hi to All, I have used the find() function of string lib in my program. Now my problem is I need to search for case insensitive sub string. Please help me to convert my code . My code sample is......... string::size_type loc=Myword.find("TaG"); //searching if tag/Tag/tAg...is present in the string if(loc!= string::npos) { //tag is present } else { //"tag absent"; } thanks in advance HimangshuS

    ----------------------------- I am a beginner

    B N 2 Replies Last reply
    0
    • H hrishiS

      Hi to All, I have used the find() function of string lib in my program. Now my problem is I need to search for case insensitive sub string. Please help me to convert my code . My code sample is......... string::size_type loc=Myword.find("TaG"); //searching if tag/Tag/tAg...is present in the string if(loc!= string::npos) { //tag is present } else { //"tag absent"; } thanks in advance HimangshuS

      ----------------------------- I am a beginner

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #2

      You have two options Override your own string type that's case insensitive: Clicky[^] Or just make lower case copies of the string and substring, and use find with those.

      Help me! I'm turning into a grapefruit! Buzzwords!

      H 1 Reply Last reply
      0
      • H hrishiS

        Hi to All, I have used the find() function of string lib in my program. Now my problem is I need to search for case insensitive sub string. Please help me to convert my code . My code sample is......... string::size_type loc=Myword.find("TaG"); //searching if tag/Tag/tAg...is present in the string if(loc!= string::npos) { //tag is present } else { //"tag absent"; } thanks in advance HimangshuS

        ----------------------------- I am a beginner

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        You can maybe try this function StrStrI. It's takes two buffers look up MSDN. So when you call this function pass in std::string likewise...

        StrStrI( &Myword[0], "TaG" );

        If successfull it will return address of the substring, else NULL. Header to include: Declared in Shlwapi.h. Import Library: Shlwapi.lib. Also make sure you don't modify the length of Myword buffer. std::string is not aware of any such updates.

        Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

        H 1 Reply Last reply
        0
        • B benjymous

          You have two options Override your own string type that's case insensitive: Clicky[^] Or just make lower case copies of the string and substring, and use find with those.

          Help me! I'm turning into a grapefruit! Buzzwords!

          H Offline
          H Offline
          hrishiS
          wrote on last edited by
          #4

          is there any function to make the string lowercase before comparing..say string a="HI How R U" if(a.find("hi")) //here I want to convert the string into lowercase n compare with lowercase hi { cout<<"found"; } thanks

          ----------------------------- I am a beginner

          1 Reply Last reply
          0
          • N Nibu babu thomas

            You can maybe try this function StrStrI. It's takes two buffers look up MSDN. So when you call this function pass in std::string likewise...

            StrStrI( &Myword[0], "TaG" );

            If successfull it will return address of the substring, else NULL. Header to include: Declared in Shlwapi.h. Import Library: Shlwapi.lib. Also make sure you don't modify the length of Myword buffer. std::string is not aware of any such updates.

            Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            H Offline
            H Offline
            hrishiS
            wrote on last edited by
            #5

            thanks for your reply... Actually if you dont mind I need little more clearence. I didnot get how do I use strstrI in the same line if(myword.find("Tag")!= string::npos) All we need is to incorporate case insensitive search in the same statement above. (Or can we pass the lowercase of myword itself so that we can check with "tag" always)...something like if(lcase(myword).find("Tag")!= string::npos) myword is of the type string thanks in advance

            ----------------------------- I am a beginner

            N 1 Reply Last reply
            0
            • H hrishiS

              thanks for your reply... Actually if you dont mind I need little more clearence. I didnot get how do I use strstrI in the same line if(myword.find("Tag")!= string::npos) All we need is to incorporate case insensitive search in the same statement above. (Or can we pass the lowercase of myword itself so that we can check with "tag" always)...something like if(lcase(myword).find("Tag")!= string::npos) myword is of the type string thanks in advance

              ----------------------------- I am a beginner

              N Offline
              N Offline
              Nibu babu thomas
              wrote on last edited by
              #6

              himangshuS wrote:

              if(myword.find("Tag")!= string::npos)

              Like this...

              if( StrStrI( &Myword[0], "Tag" ))
              {
              MessageBox( "Found \"Tag\" in Myword :)" );
              }

              Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups