c++ - 如何在MFC中分离一个CString

标签 c++ 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。只需找到 token 字符的位置(您的“=”)并获取之后的所有内容:

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
}

关于c++ - 如何在MFC中分离一个CString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56441227/

相关文章:

c++ - 英特尔MKL函数在内部调用TerminateProcess()

c++ - 如何: derived member in derived class with derived class specific methods

c++ - 为什么 CFileDialog::GetNextPathName 在文件路径很长时不起作用?

performance - 如何加快 MFC 色带应用程序的加载时间

c++ - 如何在 C++/MFC/GDI 中创建一个非常大的位图

c++ - 缺少 DLL 问题

c++ - 如何从Windows for Linux(Dev-Cpp)编译C++代码

c++ - 当光标移到子窗口上时避免 WM_MOUSELEAVE

c++ - 未调用 QT 析构函数

c - 如何在 ListView 中插入彩色文本?