当前位置: 代码迷 >> 综合 >> 字符串中提取数字算法
  详细解决方案

字符串中提取数字算法

热度:3   发布时间:2024-01-17 15:10:21.0

背景:需要从字符串中取出所有整数,暂不考虑负数。


CString strText = _T("1/2/17/18");
vector<CString>& vNumList;

const char* pText = strText.GetBuffer(strText.GetLength());
CString strTemp = _T("");
while (*pText != '\0')
{
    if (*pText >='0' && *pText <= '9')
    {
        strTemp += *pText;
    }
    else if (!strTemp.IsEmpty())
    {
        vNumList.push_back(strTemp);
        strTemp = _T("");
    }


    pText++;
}

if (!strTemp.IsEmpty())
{
    vNumList.push_back(strTemp);
}
  相关解决方案