With previous versions of MFC, CString::Left(-1) returned an empty string.
It was therefore possible to do the following (which was very practical):
CString s1 = "hello-hello";
CString s2 = "hello hello";
CString sSub = "-";
CString sRes1 = s1.Right( s1.Find(sSubString) ); // Find() returns 5,
sRes1 == "hello"
CString sRes2 = s2.Right( s2.Find(sSubString) ); // Find() returns -1,
sRes2 == ""
In MFC 7 this (the results in an exception (resp. ASSERT in debug builds).
Is this a bug in MFC/CString or is it by design?
Thanks for your comments,
Martin Bischoff
--
To send an eMail remove NO SPAM
CString s1, s2, sSub, sRes1, sRes2;
s1 = "hello-hello";
s2 = "hello hello";
sSub = "-";
sRes1 = s1.Left( s1.Find(sSubString) );
/* s1.Find() returns 5, sRes1 == "hello" */
sRes2 = s2.Left( s2.Find(sSubString) );
/* s2.Find() returns -1, sRes2 is now "" */
Thanks,
Not really, there is still no declaration for sSubString, but let me assume,
that should be sSub...
> CString s1, s2, sSub, sRes1, sRes2;
> s1 = "hello-hello";
> s2 = "hello hello";
> sSub = "-";
>
> sRes1 = s1.Left( s1.Find(sSubString) );
> /* s1.Find() returns 5, sRes1 == "hello" */
>
> sRes2 = s2.Left( s2.Find(sSubString) );
> /* s2.Find() returns -1, sRes2 is now "" */
Where should Find() find a dash in "hello hello"?
Heinz
>
>>CString s1, s2, sSub, sRes1, sRes2;
>>s1 = "hello-hello";
>>s2 = "hello hello";
>>sSub = "-";
>>
>>sRes1 = s1.Left( s1.Find(sSub) );
>>/* s1.Find() returns 5, sRes1 == "hello" */
>>
>>sRes2 = s2.Left( s2.Find(sSub) );
>>/* s2.Find() returns -1, sRes2 is now "" */
>
>
> Where should Find() find a dash in "hello hello"?
>
Find will return 5 in the first case and -1 in the second (because the
substring can't be found). No problem so far.
with MFC prior to version 7:
s1.Left(5); // --> returns "hello"
s2.Left(-1); // --> returns "" !!
with MFC 7 (here comes the problem):
s1.Left(5); // --> still returns "hello"
s2.Left(-1); // --> ASSERT / unhandled exception !!
Thanks for your comments,
>with MFC prior to version 7:
>s1.Left(5); // --> returns "hello"
>s2.Left(-1); // --> returns "" !!
>
>with MFC 7 (here comes the problem):
>s1.Left(5); // --> still returns "hello"
>s2.Left(-1); // --> ASSERT / unhandled exception !!
It seems to me that you are/were relying upon an implementation detail/bug for
the correct behavior of your program. Nowhere in the CString::Left()
documentation do I see its behavior defined when the parameter is negative.
Is there any particular reason you can't write your code according to the
documented class interface?
Jon
s2 = s1.Left(s1.Find(s));
-or-
s2 = s1.Right(s1.Find(s));
In alle these lines, s1.Find() might return -1, which means that I have
to check all occurrences :-(
Martin
--
To send an e-mail remove NO SPAM