如何从CString中仅提取数字 [英] How to extract only numbers from CString

查看:125
本文介绍了如何从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.

推荐答案

您必须编写自己的解析器才能执行此操作。假设元素由空格分隔并且所有数字都是浮点数,这可以使用 strtod()来完成:

You must write your own parser to do this. Assuming that the elements are separated by spaces and all numbers are floating point numbers, this can be done using strtod():
// 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中仅提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆