简体   繁体   English

如何在MFC中分离CString

[英]How to separate a CString in MFC

I have a string like this: DialogTitle = IDD_SETTING_DLG in a save file (i have already stored it in an array called m_TextArray ). 我有一个这样的字符串: DialogTitle = IDD_SETTING_DLG在保存文件中(我已经将其存储在名为m_TextArray的数组中)。

Now i want to get the "IDD_SETTING_DLG" part(or at least " IDD_SETTING_DLG" ) and store it in a CString variable. 现在,我想获取"IDD_SETTING_DLG"部分(或至少" IDD_SETTING_DLG" )并将其存储在CString变量中。 I used the Tokenize method but it didn't work. 我使用了Tokenize方法,但是没有用。

Here are my codes: 这是我的代码:

BOOL CTab1::OnInitDialog()
{
    UpdateData();
    ReadSaveFile();
    SetTabDescription();
    UpdateData(FALSE);
    return TRUE;
}

void CTab1::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_SHOWDES, m_ShowDes);
}

void CTab1::ReadSaveFile()
{
    if (!SaveFile.Open(SFLocation, CFile::modeRead | CFile::shareDenyWrite, &ex))
    {
        ReadSettingFile();
    }
    else
    {
        for (int i = 0; i < 100; i++)
        {
            SaveFile.ReadString(ReadLine);
            m_TextArray[i] = ReadLine.GetString();
        }
    }
}
void CTab1::SetTabDescription() //m_TextArray[2] is where i stored the text
{
    Position = 0;
    Seperator = _T("=");

    m_ShowDes = m_TextArray[2].Tokenize(Seperator, Position);

    while (!m_ShowDes.IsEmpty())
    {
                // get the next token
        m_ShowDes = m_TextArray[2].Tokenize(Seperator, Position);
    }
}

Anyone solution or hint would be very appreciated. 任何人的解决方案或提示将不胜感激。

Since you're merely looking for the part of the string that occurs after a token, there's no need to use Tokenize . 由于您只是在寻找在令牌后面出现的字符串部分,因此无需使用Tokenize Just find the position of the token character (your " = ") and get everything after that: 只需找到令牌字符的位置(您的“ = ”),然后获取所有信息:

void CTab1::SetTabDescription() //m_TextArray[2] is where i stored the text
{
    CString separator = _T("=");
    CString source = m_TextArray[2];

    // Get position of token...
    int position = source.Find(separator);

    // If token is found...
    if (position > -1 && source.GetLength() > position)
        m_ShowDes = source.Mid(position + 1);  // extract everything after token
}

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

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