Search code examples
c++visual-c++mfccstring

How to manipulate CString's content


guys, I have a problem, please help me! I have a CString variable which will receieve from Database,and the data may like this:(8)(9)(10)(11) or more.

Now I want to change every number in CString,for example, add 1,the outcome should like this:

CString Data; the contest of CString data variable should be changed.

Before: (8)(9)(10)(11)

After: (9)(10)(11)(12)

I've tried Data.GetAt(i),but it returns a const pointer and I can't change it.

I konw maybe Data.GetBuffer() can get a pointer to manipulate CString,but I don't know how to do it.

So please help me ! Thanks a lot!


Solution

  • Don't change in-place. Specifically in your example, when you change (9) to (10) it needs 1 extra character of buffer space. Extract all the values somehow (a std::list of int perhaps), add whatever numbers you need, and then re-assemble into a string, and write it back all at once.