CString::Replace hell
-
I am having a rather anoying problem with my CString's. Consider the following code:
CString strTest = _T("hello world");
ASSERT(strTest.Replace(_T("world"), _T("universe")) > 0)
ASSERT(strTest.Find(_T("world")) == -1)
Now, that the last ASSERT will always fail (indicating that "world" is present in the string). This can be verified by sticking an AfxMessageBox call in there too. Why (or rather how) can this happen when the replace call always succeeds with one replacement. :confused: Cheers, James Millson
-
I am having a rather anoying problem with my CString's. Consider the following code:
CString strTest = _T("hello world");
ASSERT(strTest.Replace(_T("world"), _T("universe")) > 0)
ASSERT(strTest.Find(_T("world")) == -1)
Now, that the last ASSERT will always fail (indicating that "world" is present in the string). This can be verified by sticking an AfxMessageBox call in there too. Why (or rather how) can this happen when the replace call always succeeds with one replacement. :confused: Cheers, James Millson
I think the culprit is your last ASSERT statement, CString's Find member returns -1, if the sub string was not found. It should be:
ASSERT ( strTest.Find ( _T ( "world" ) ) != -1 )
-Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;)
-
I think the culprit is your last ASSERT statement, CString's Find member returns -1, if the sub string was not found. It should be:
ASSERT ( strTest.Find ( _T ( "world" ) ) != -1 )
-Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;)
-
Nope, It should be just as James wrote it... You want the ASSERT to PASS if "world" is NOT found. In fact I compiled this snippet and both asserts pass just fine on my system. Steve T.
Your right Steve, I have no idea what it was that I thought he was asking... This kind of thing has been happening all day, I must be on crack or something ;) cheers, -Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;)
-
Nope, It should be just as James wrote it... You want the ASSERT to PASS if "world" is NOT found. In fact I compiled this snippet and both asserts pass just fine on my system. Steve T.
That's what I thought :confused:. I can only get it to pass on my system if I do:
CString strTemp = _T("hello world");
CString strTest = new CString(strTemp).GetBuffer(0);
ASSERT(strTest.Replace(_T("world"), _T("universe")) > 0)
ASSERT(strTest.Find(_T("world")) == -1)And I don't know if that's a very good way of doing it. And the problem only occurs in the current usuage (i.e. it works elsewhere in the project). But i've stepped backwards and forwards for hours and nothing else looks wrong, and nothing is there to indicate it is. How very odd :(. Cheers James Millson p.s. Oh, and yes, I made up the actual text in the example above, but other than that it is the same. In both cases the equivalent of "world" should not be present in the string.