本文介绍了如何从CString中仅提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All ..



在从CString中仅提取数字时遇到问题。我有一个像

的字符串示例1:CString cs =1697.7 ST 8.2 BIIU__LL 1697.60 1698.30

示例2:CString cs1 =16588 ST 78 BJ89__LL 1685.90 1623.87



我想从这个字符串中仅提取数字。输出如:

1697.7 8.2 1697.60 1698.30



如何实现这一点,请建议/指导我..





谢谢大家。

Hello All..

Am facing problem in extracting only numbers from CString. Am having a string like
Example 1: CString cs = "1697.7 ST 8.2 BIIU__LL 1697.60 1698.30 "
Example 2: CString cs1 = "16588 ST 78 BJ89__LL 1685.90 1623.87 "

And i want to extract only numbers from this string.Output like :
1697.7 8.2 1697.60 1698.30

How to achieve this, please suggest/guide me..


Thank you all.

推荐答案

// Get space separated floating point number as CString object.
// Empty string if not a floating point value.
// Returns pointer to next space, trailing NULL char, or NULL
LPCTSTR GetNumberStr(CString& str, LPCTSTR s) const
{
    while (*s <= _T(' '))
        ++s;
    LPTSTR lpszEnd;
    _tcstod(s, &lpszEnd);
    // Floating point or int value if conversion stopped at space or end of string
    if (*lpszEnd <= _T(' '))
        str.SetString(s, static_cast<int>(lpszEnd - s));
    else
    {
        str = _T("");
        // This may return NULL!
        lpszEnd = _tcschr(s, _T(' '));
    }
    return lpszEnd;
}

void Parse(CString& strOut, const CString& strIn) const
{
    strOut = _T("");
    LPCTSTR s = strIn.GetString();
    while (s && *s)
    {
        CString str;
        s = GetNumberStr(str, s);
        if (!str.IsEmpty())
        {
            if (!strOut.IsEmpty())
                strOut += _T(' ');
            strOut += str;
        }
    }
}


这篇关于如何从CString中仅提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:16