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. finding a string in a CString

finding a string in a CString

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
5 Posts 4 Posters 0 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    can anyone please tell me what the simplest way is to see if a ceratin string is in another CString, for example: i have a CString with value: "coffee smells terrible" and i want to see if "err" is in that string(in "terrible"), how do i do that? thanx Kun

    S B 2 Replies Last reply
    0
    • L Lost User

      can anyone please tell me what the simplest way is to see if a ceratin string is in another CString, for example: i have a CString with value: "coffee smells terrible" and i want to see if "err" is in that string(in "terrible"), how do i do that? thanx Kun

      S Offline
      S Offline
      Steve Maier
      wrote on last edited by
      #2

      You can use the Find function of CString. So you would have something like this:

       int pos;
       CString buffer;
       buffer = "coffee smells terrible";
       pos = buffer.Find("err");
       if(pos != -1)
       {
            // found the string
       }    
      

      This should work for both MFC and WTL. Steve Maier, MCSD

      B 1 Reply Last reply
      0
      • S Steve Maier

        You can use the Find function of CString. So you would have something like this:

         int pos;
         CString buffer;
         buffer = "coffee smells terrible";
         pos = buffer.Find("err");
         if(pos != -1)
         {
              // found the string
         }    
        

        This should work for both MFC and WTL. Steve Maier, MCSD

        B Offline
        B Offline
        Baafie
        wrote on last edited by
        #3

        well isn't this nice...

        1 Reply Last reply
        0
        • L Lost User

          can anyone please tell me what the simplest way is to see if a ceratin string is in another CString, for example: i have a CString with value: "coffee smells terrible" and i want to see if "err" is in that string(in "terrible"), how do i do that? thanx Kun

          B Offline
          B Offline
          Baafie
          wrote on last edited by
          #4

          CString strCoffee = "coffee smells terrible"; int iSearch = strCoffee.find("err"); if (iSearch != -1) { //found it. } else { //couldn't find it. }

          realJSOPR 1 Reply Last reply
          0
          • B Baafie

            CString strCoffee = "coffee smells terrible"; int iSearch = strCoffee.find("err"); if (iSearch != -1) { //found it. } else { //couldn't find it. }

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            Ummm, you got the method name case wrong...

            CString sBuffer(_T("The coffee smells terrible"));
            if (sBuffer.Find("err") >= 0) // "Find" starts with a capital "F"
            {
            // string was found
            }
            else
            {
            // string not found
            }

            Also, keep in mind that string searches in a CString are case-sensitive, so if that matters, you may want to use the CString::MakeLower() function befoire running your search.

            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