如何在 MFC 中分离 CString

How to separate a CString in MFC

本文关键字:分离 CString MFC      更新时间:2023-10-16

我有这样的字符串: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
}