CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2000
    Location
    NY, USA Rochester
    Posts
    23

    CString comparison

    Hello. I want to compare a CString object to a char[] with a string in it. Can I just use the CString == operator or do I have to convert the string or CString first before I can compare them? Thanks in advance.

    T


  2. #2
    Join Date
    Oct 1999
    Location
    Broomfield, CO
    Posts
    3,382

    Re: CString comparison

    The CString operator== compares CStrings, not CStrings with something else.

    Either convert your chars to a CString, or pull out the CString's string and use things like strcmp().


  3. #3
    Join Date
    Sep 2000
    Location
    NY, USA Rochester
    Posts
    23

    Re: CString comparison

    What is the best way to pull out the CString string? That's the current problem I am wrestling with. Should I just cast it as a LPTCSTR and place it in a buffer and then strcmp the temp buffer with the other string? Thanks a lot for your help.


  4. #4
    Join Date
    Oct 1999
    Location
    Broomfield, CO
    Posts
    3,382

    Re: CString comparison

    It might be easier to convert the char[] to a CString and use its comparison functions.


    // example of char[] to CString
    SYSTEMTIME sysTime;
    GetLocalTime(&sysTime);
    char pIToA[5];
    itoa(sysTime.wMonth, pIToA, 10);
    CString cMonth(pIToA);
    itoa( sysTime.wDay, pIToA, 10);
    CString cDay(pIToA);
    itoa(sysTime.wYear, pIToA, 10);
    CString cYear(pIToA);
    CString cLine = cMonth + "/" + cDay + "/" + cYear.Right(2);







  5. #5
    Join Date
    Sep 2000
    Location
    NY, USA Rochester
    Posts
    23

    Re: CString comparison

    Hmm that might be a viable solution. But if I need to go CString to char[] do you recommend anything? The application I am working on has existed for 8 years of hand me downs and is not easy to change the style on. Thanks for your help.


  6. #6
    Join Date
    Oct 1999
    Location
    Broomfield, CO
    Posts
    3,382

    Re: CString comparison

    Use the LPCTSTR operator of the CString class and do your strcmp.


  7. #7
    Join Date
    Sep 2000
    Location
    NY, USA Rochester
    Posts
    23

    Re: CString comparison

    Thanks I gave you some points!


  8. #8
    Join Date
    Jun 1999
    Location
    Miami, FL
    Posts
    972

    Re: CString comparison

    Actually Bob, there are 3 versions of the global equality operator, according to the help:

    BOOL operator ==( const CString& s1, const CString& s2 );
    BOOL operator ==( const CString& s1, LPCTSTR s2 );
    BOOL operator ==( LPCTSTR s1, const CString& s2 );



    So you CAN compare CStrings to char arrays without having to convert them.

    Regards,
    Alvaro


  9. #9
    Join Date
    Jun 1999
    Location
    Miami, FL
    Posts
    972

    Re: CString comparison

    No conversion's necessary. There are 2 possible equality operators that will handle the comparison:


    BOOL operator ==( const CString& s1, LPCTSTR s2 );
    BOOL operator ==( LPCTSTR s1, const CString& s2 );



    Regards,
    Alvaro


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured