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

How to compare string..

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
22 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.
  • S Offline
    S Offline
    Shah Satish
    wrote on last edited by
    #1

    Hi!!! I have one simple question.. How i can compare strin.. code is if(x1.compareNocase(s1 || s2) { do something } Here if x1 is equal to s1 or s2 then do something in my code.. How i can compare..?? as in || is right or wrong?? Shah Satish

    M P 2 Replies Last reply
    0
    • S Shah Satish

      Hi!!! I have one simple question.. How i can compare strin.. code is if(x1.compareNocase(s1 || s2) { do something } Here if x1 is equal to s1 or s2 then do something in my code.. How i can compare..?? as in || is right or wrong?? Shah Satish

      M Offline
      M Offline
      Mila025
      wrote on last edited by
      #2

      Hi, You trying to be too fast ;-) if ( x1.CompareNoCase( s1 ) || x1.CompareNoCase( s2 ) ) { ... }

      ----------- Mila

      S 1 Reply Last reply
      0
      • S Shah Satish

        Hi!!! I have one simple question.. How i can compare strin.. code is if(x1.compareNocase(s1 || s2) { do something } Here if x1 is equal to s1 or s2 then do something in my code.. How i can compare..?? as in || is right or wrong?? Shah Satish

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        Shah Satish wrote:

        if(x1.compareNocase(s1 || s2) { do something }

        if ((x1.CompareNoCase(s1)== 0) || (x1.CompareNoCase(s2)== 0))
        {
        //do something
        }

        Shah Satish wrote:

        as in || is right or wrong??

        Here it is wrong, using your way.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        S M 2 Replies Last reply
        0
        • M Mila025

          Hi, You trying to be too fast ;-) if ( x1.CompareNoCase( s1 ) || x1.CompareNoCase( s2 ) ) { ... }

          ----------- Mila

          S Offline
          S Offline
          Shah Satish
          wrote on last edited by
          #4

          I have code.. If(Loopname.CompareNocase(x1)==0 || Loopname.CompareNocase(mytext->contents)==0). but its not working

          M D 2 Replies Last reply
          0
          • P prasad_som

            Shah Satish wrote:

            if(x1.compareNocase(s1 || s2) { do something }

            if ((x1.CompareNoCase(s1)== 0) || (x1.CompareNoCase(s2)== 0))
            {
            //do something
            }

            Shah Satish wrote:

            as in || is right or wrong??

            Here it is wrong, using your way.

            Prasad Notifier using ATL | Operator new[],delete[][^]

            S Offline
            S Offline
            Shah Satish
            wrote on last edited by
            #5

            I have code.. If(Loopname.CompareNocase(x1)==0 || Loopname.CompareNocase(mytext->contents)==0). but its not working..

            P 1 Reply Last reply
            0
            • S Shah Satish

              I have code.. If(Loopname.CompareNocase(x1)==0 || Loopname.CompareNocase(mytext->contents)==0). but its not working..

              P Offline
              P Offline
              prasad_som
              wrote on last edited by
              #6

              Shah Satish wrote:

              but its not working..

              Means what ? Can you try modifying it to,

              If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0))
              {
              //do something
              }

              Prasad Notifier using ATL | Operator new[],delete[][^]

              S 2 Replies Last reply
              0
              • P prasad_som

                Shah Satish wrote:

                but its not working..

                Means what ? Can you try modifying it to,

                If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0))
                {
                //do something
                }

                Prasad Notifier using ATL | Operator new[],delete[][^]

                S Offline
                S Offline
                Shah Satish
                wrote on last edited by
                #7

                No..Not modified..This is my actual code..But i dont know its not working.

                1 Reply Last reply
                0
                • P prasad_som

                  Shah Satish wrote:

                  but its not working..

                  Means what ? Can you try modifying it to,

                  If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0))
                  {
                  //do something
                  }

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  S Offline
                  S Offline
                  Shah Satish
                  wrote on last edited by
                  #8

                  ie only one case is wroking properly..Both together is not working.. If( (Loopname.CompareNocase(x1)==0)---This is working (Loopname.CompareNocase(mytext->contents)==0))--This is working If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0))--But together is not working..

                  T P 2 Replies Last reply
                  0
                  • S Shah Satish

                    I have code.. If(Loopname.CompareNocase(x1)==0 || Loopname.CompareNocase(mytext->contents)==0). but its not working

                    M Offline
                    M Offline
                    Mila025
                    wrote on last edited by
                    #9

                    Hi, hm... interesting - and what type are x1 and mytext->contents -> is it CString too ? (I'm thinking about e.g. you use VS2005 where default is UNICODE defined and you try to compare default CString with char *) Besides you can check what exactly return particular comparing functions (if equal should returns NULL)

                    ----------- Mila

                    1 Reply Last reply
                    0
                    • S Shah Satish

                      ie only one case is wroking properly..Both together is not working.. If( (Loopname.CompareNocase(x1)==0)---This is working (Loopname.CompareNocase(mytext->contents)==0))--This is working If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0))--But together is not working..

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      you are using a logical OR operator, which returns TRUE if one of the 2 operands is TRUE. so if its 1st part (1st comparison) is TRUE, then the 2nd one is not executed... are you sure you want to use a OR ? don't you need a AND ( && ) instead ?


                      Don't know where to start ?
                      Refer the Forums Guidelines and ask a friend

                      [VisualCalc 3.0][Flags Beginner's Guide]

                      S 1 Reply Last reply
                      0
                      • S Shah Satish

                        ie only one case is wroking properly..Both together is not working.. If( (Loopname.CompareNocase(x1)==0)---This is working (Loopname.CompareNocase(mytext->contents)==0))--This is working If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0))--But together is not working..

                        P Offline
                        P Offline
                        prasad_som
                        wrote on last edited by
                        #11

                        Shah Satish wrote:

                        But together is not working..

                        What does this mean ? Remember, Loopname.CompareNocase(mytext->contents) will not be called if Loopname.CompareNocase(x1) returns 0. (or rule)

                        Prasad Notifier using ATL | Operator new[],delete[][^]

                        S 2 Replies Last reply
                        0
                        • P prasad_som

                          Shah Satish wrote:

                          But together is not working..

                          What does this mean ? Remember, Loopname.CompareNocase(mytext->contents) will not be called if Loopname.CompareNocase(x1) returns 0. (or rule)

                          Prasad Notifier using ATL | Operator new[],delete[][^]

                          S Offline
                          S Offline
                          Shah Satish
                          wrote on last edited by
                          #12

                          mytext->contents is a pointer.. as in AcDbMtext *mytext ; mytext->Contents; what to do?? How t compare it?

                          T D 2 Replies Last reply
                          0
                          • P prasad_som

                            Shah Satish wrote:

                            But together is not working..

                            What does this mean ? Remember, Loopname.CompareNocase(mytext->contents) will not be called if Loopname.CompareNocase(x1) returns 0. (or rule)

                            Prasad Notifier using ATL | Operator new[],delete[][^]

                            S Offline
                            S Offline
                            Shah Satish
                            wrote on last edited by
                            #13

                            Ya..I have to use only "or" function.. but among them one is pointer and one is string.. mytext->contents is a pointer.. as in AcDbMtext *mytext ; mytext->Contents; If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0)) x1 is cstring and (mytext->contents)is a pointer.. what to do?? How t compare it?

                            P 1 Reply Last reply
                            0
                            • T toxcct

                              you are using a logical OR operator, which returns TRUE if one of the 2 operands is TRUE. so if its 1st part (1st comparison) is TRUE, then the 2nd one is not executed... are you sure you want to use a OR ? don't you need a AND ( && ) instead ?


                              Don't know where to start ?
                              Refer the Forums Guidelines and ask a friend

                              [VisualCalc 3.0][Flags Beginner's Guide]

                              S Offline
                              S Offline
                              Shah Satish
                              wrote on last edited by
                              #14

                              Ya..I have to use only "or" function.. but among them one is pointer and one is string.. mytext->contents is a pointer.. as in AcDbMtext *mytext ; mytext->Contents; If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0)) x1 is cstring and (mytext->contents)is a pointer.. what to do?? How t compare it?

                              T 1 Reply Last reply
                              0
                              • S Shah Satish

                                mytext->contents is a pointer.. as in AcDbMtext *mytext ; mytext->Contents; what to do?? How t compare it?

                                T Offline
                                T Offline
                                toxcct
                                wrote on last edited by
                                #15

                                you didn't read the answers :zzz:


                                Don't know where to start ?
                                Refer the Forums Guidelines and ask a friend

                                [VisualCalc 3.0][Flags Beginner's Guide]

                                1 Reply Last reply
                                0
                                • S Shah Satish

                                  Ya..I have to use only "or" function.. but among them one is pointer and one is string.. mytext->contents is a pointer.. as in AcDbMtext *mytext ; mytext->Contents; If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0)) x1 is cstring and (mytext->contents)is a pointer.. what to do?? How t compare it?

                                  T Offline
                                  T Offline
                                  toxcct
                                  wrote on last edited by
                                  #16

                                  Loopname is a CString, right ? then look at the signature of CString::CompareNoCase(). it gets a PCXSTR parameter (so, pointer to char). so ther's no problem in doing so. if your code doesn't work, it is because of what i notified you previously. have you tried inverting the two statements in the OR operation (C-style string compared first) ?


                                  Don't know where to start ?
                                  Refer the Forums Guidelines and ask a friend

                                  [VisualCalc 3.0][Flags Beginner's Guide]

                                  1 Reply Last reply
                                  0
                                  • P prasad_som

                                    Shah Satish wrote:

                                    if(x1.compareNocase(s1 || s2) { do something }

                                    if ((x1.CompareNoCase(s1)== 0) || (x1.CompareNoCase(s2)== 0))
                                    {
                                    //do something
                                    }

                                    Shah Satish wrote:

                                    as in || is right or wrong??

                                    Here it is wrong, using your way.

                                    Prasad Notifier using ATL | Operator new[],delete[][^]

                                    M Offline
                                    M Offline
                                    Maximilien
                                    wrote on last edited by
                                    #17

                                    if this does not work, split the CompareNoCase and the if in different statements and debug each statement separatly.

                                    bool b1 = (x1.CompareNoCase(s1)== 0);
                                    bool b2 = (x1.CompareNoCase(s2)== 0);
                                    if ( b1 || b2 )
                                    {
                                    }


                                    Maximilien Lincourt Your Head A Splode - Strong Bad

                                    S 1 Reply Last reply
                                    0
                                    • S Shah Satish

                                      mytext->contents is a pointer.. as in AcDbMtext *mytext ; mytext->Contents; what to do?? How t compare it?

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

                                      What you've shown in now way indicates that contents is a pointer. While mytext is indeed a pointer, you've shown nothing of its contents member. If contents is indeed a pointer, what's it a pointer to?


                                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                                      "Judge not by the eye but by the heart." - Native American Proverb

                                      1 Reply Last reply
                                      0
                                      • S Shah Satish

                                        I have code.. If(Loopname.CompareNocase(x1)==0 || Loopname.CompareNocase(mytext->contents)==0). but its not working

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

                                        Shah Satish wrote:

                                        but its not working

                                        What is this supposed to mean? If one of the calls to CompareNocase() is returning a non-zero value, which you have yet to verify, that indicates the strings do not match. Whether the strings match or not, your code is syntactically correct.


                                        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                                        "Judge not by the eye but by the heart." - Native American Proverb

                                        1 Reply Last reply
                                        0
                                        • S Shah Satish

                                          Ya..I have to use only "or" function.. but among them one is pointer and one is string.. mytext->contents is a pointer.. as in AcDbMtext *mytext ; mytext->Contents; If( (Loopname.CompareNocase(x1)==0) || (Loopname.CompareNocase(mytext->contents)==0)) x1 is cstring and (mytext->contents)is a pointer.. what to do?? How t compare it?

                                          P Offline
                                          P Offline
                                          prasad_som
                                          wrote on last edited by
                                          #20

                                          Come on Satish ! You are still not reading answers well and replying to our queries well. 1. What does not working mean ? You need to be very clear. 2. Have you taken in to consideration when using || operator second operand will be looked only when first is false?

                                          Shah Satish wrote:

                                          x1 is cstring and (mytext->contents)is a pointer..

                                          It should not matter here.

                                          Prasad Notifier using ATL | Operator new[],delete[][^]

                                          S 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