如何在MFC中分离一个CString

How to separate a CString in MFC

我有一个这样的字符串:DialogTitle = IDD_SETTING_DLG 在一个保存文件中(我已经将它存储在一个名为 m_TextArray 的数组中)。

现在我想获取 "IDD_SETTING_DLG" 部分(或至少 " IDD_SETTING_DLG")并将其存储在 CString 变量中。我使用了 Tokenize 方法,但没有用。

这是我的代码:

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

任何解决方案或提示都将不胜感激。

由于您只是在查找字符串中出现在标记之后的部分,因此无需使用 Tokenize。只需找到令牌字符的位置(您的“=”)并获取之后的所有内容:

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
}