mfc copy certain sections of a CString

岁酱吖の 提交于 2019-12-02 03:53:32

问题


Let's say I have a CString variable carrying the string "Bob Evans". I want to copy from position 4 until the end of the original CString to a new CString, but I am having trouble finding semantics examples for this:

CString original("Bob Evans");
// Below is what I'm trying to do
// CString newStr = original.copy(4, original.GetLength());

I have also thought about copying the variable original to a STL C++ string, but achieving this isn't all that easy either in terms of the conversion. What would be your advice regarding this? I could make the string to be stored in a STL string to begin with, but this would be one of the last resort as I didn't want to restructure a lot of code just to store the data in STL string instead of CString. Thanks in advance.


回答1:


newStr = original.Mid(4);



回答2:


It isn't all that hard to turn a CString into a standard string; the only glitch is that you're probably using Unicode if you take the default settings for your MFC program. That means you'll want to use std::wstring instead of std::string.

I haven't tested this, but I think the default conversions will let this 'just work'. Otherwise cast the CString to LPCTSTR.

std::wstring copyOfOriginal(original);


来源:https://stackoverflow.com/questions/1014550/mfc-copy-certain-sections-of-a-cstring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!