简体   繁体   English

mfc复制CString的某些部分

[英]mfc copy certain sections of a CString

Let's say I have a CString variable carrying the string "Bob Evans". 假设我有一个CString变量,带有字符串“ 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: 我想从位置4复制到原始CString的末尾,再复制到新的CString,但是我很难为此找到语义示例:

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. 我还考虑过将变量original复制到STL C ++字符串中,但是就转换而言,实现此目标并非易事。 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. 我可以先将字符串存储在STL字符串中,但这将是最后的选择之一,因为我不想重组很多代码,而只是将数据存储在STL字符串中而不是CString中。 Thanks in advance. 提前致谢。

newStr = original.Mid(4);

It isn't all that hard to turn a CString into a standard string; CString转换为标准字符串并不难; the only glitch is that you're probably using Unicode if you take the default settings for your MFC program. 唯一的问题是,如果您为MFC程序采用默认设置,则可能正在使用Unicode。 That means you'll want to use std::wstring instead of std::string . 这意味着您将要使用std::wstring而不是std::string

I haven't tested this, but I think the default conversions will let this 'just work'. 我尚未对此进行测试,但我认为默认转换将使其“正常工作”。 Otherwise cast the CString to LPCTSTR. 否则,将CString强制转换为LPCTSTR。

std::wstring copyOfOriginal(original);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM