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. CString::Replace hell

CString::Replace hell

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 3 Posters 1 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.
  • J Offline
    J Offline
    James Millson
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • J 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

      B Offline
      B Offline
      Ben Burnett
      wrote on last edited by
      #2

      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" ;)

      S 1 Reply Last reply
      0
      • B Ben Burnett

        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" ;)

        S Offline
        S Offline
        Steve T
        wrote on last edited by
        #3

        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.

        B J 2 Replies Last reply
        0
        • S Steve T

          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.

          B Offline
          B Offline
          Ben Burnett
          wrote on last edited by
          #4

          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" ;)

          1 Reply Last reply
          0
          • S Steve T

            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.

            J Offline
            J Offline
            James Millson
            wrote on last edited by
            #5

            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.

            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