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. compare string

compare string

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 6 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.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    Hi all, would anyone happen to know the equivalent of comparing a string declared variable or character? Sort of like strstr or strcmp, i just cant find which one is used for C-like string. Thanx in advance!

    M 1 Reply Last reply
    0
    • D dellthinker

      Hi all, would anyone happen to know the equivalent of comparing a string declared variable or character? Sort of like strstr or strcmp, i just cant find which one is used for C-like string. Thanx in advance!

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      To compare what kind of strings (sorry, I'm not following :))? Mark

      Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

      D 1 Reply Last reply
      0
      • M Mark Salsbery

        To compare what kind of strings (sorry, I'm not following :))? Mark

        Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

        D Offline
        D Offline
        dellthinker
        wrote on last edited by
        #3

        string somecode; strstr and strcmp does not work for string unless its declared as a char I dont know what to search for because i've already tried Google and nothing came up that refers to what im looking for.

        M R P M 4 Replies Last reply
        0
        • D dellthinker

          string somecode; strstr and strcmp does not work for string unless its declared as a char I dont know what to search for because i've already tried Google and nothing came up that refers to what im looking for.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Stuff like this? strcmp(str.c_str(), str2.c_str()); wcscmp(str.c_str(), str2.c_str()); operator== ( <string> )[^] basic_string::compare[^] Mark

          Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

          1 Reply Last reply
          0
          • D dellthinker

            string somecode; strstr and strcmp does not work for string unless its declared as a char I dont know what to search for because i've already tried Google and nothing came up that refers to what im looking for.

            R Offline
            R Offline
            Ram Murali
            wrote on last edited by
            #5

            You may try the following CString s1, s2; if ( s1.GetLength() != s2.GetLengtth() ) // Not equal else { for( int i = 0; i < s1.GetLength(); i++ ) { if ( (Byte) s1.GetAt(i) != (BYTE) s2.GetAt(i) ) break; } if ( i != s1.GetLength() ) // String Not equal. else // String equal. } Hope this will help u, if ur string length is small.

            Regards, Ram

            D 1 Reply Last reply
            0
            • D dellthinker

              string somecode; strstr and strcmp does not work for string unless its declared as a char I dont know what to search for because i've already tried Google and nothing came up that refers to what im looking for.

              P Offline
              P Offline
              Paresh Chitte
              wrote on last edited by
              #6

              Try using CString::Compare.

              // example for CString::Compare
              CString s1( "abc" );
              CString s2( "abd" );
              ASSERT( s1.Compare( s2 ) == -1 ); // Compare with another CString.
              ASSERT( s1.Compare( "abe" ) == -1 ); // Compare with LPTSTR string.
              

              Regards, Paresh.

              1 Reply Last reply
              0
              • D dellthinker

                string somecode; strstr and strcmp does not work for string unless its declared as a char I dont know what to search for because i've already tried Google and nothing came up that refers to what im looking for.

                M Offline
                M Offline
                Manish Paul
                wrote on last edited by
                #7

                "string" class has overloaded "==" operator. You can directly use the == operator. Eg: string s1("hi"); string s2("hello"); if (s1==s2) { .... } else { .... }

                1 Reply Last reply
                0
                • R Ram Murali

                  You may try the following CString s1, s2; if ( s1.GetLength() != s2.GetLengtth() ) // Not equal else { for( int i = 0; i < s1.GetLength(); i++ ) { if ( (Byte) s1.GetAt(i) != (BYTE) s2.GetAt(i) ) break; } if ( i != s1.GetLength() ) // String Not equal. else // String equal. } Hope this will help u, if ur string length is small.

                  Regards, Ram

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Why do people continue to recommend using CString methods when the OP is using a string object? It just doesn't make sense. :rolleyes: Furthermore, why would you not just use CString's Compare(), CompareNoCase(), or == operator?


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  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