-
October 2nd, 2000, 10:28 AM
#1
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
-
October 2nd, 2000, 11:09 AM
#2
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().
-
October 2nd, 2000, 11:15 AM
#3
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.
-
October 2nd, 2000, 11:22 AM
#4
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);
-
October 2nd, 2000, 11:25 AM
#5
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.
-
October 2nd, 2000, 11:33 AM
#6
Re: CString comparison
Use the LPCTSTR operator of the CString class and do your strcmp.
-
October 2nd, 2000, 12:59 PM
#7
Re: CString comparison
Thanks I gave you some points!
-
October 2nd, 2000, 01:41 PM
#8
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
-
October 2nd, 2000, 01:43 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|