从字符串中提取整数 [英] extract an integer from a string

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

问题描述

嗨!



i希望从字符串中提取整数,但我不知道如何,例如,如果我有下面的字符串:

hi !

i want to extract an integer from a string but i dont know how, for example if i have the folowing string :

"100Mbps"

我只想得到结果:< b> 100



感谢您的帮助:)

i just want to get as a result : 100

thank you for your help :)

推荐答案

您可以使用此代码:

You can use this code:
std::string num ("");
std::string str ("100Mbps");

for (int i=0; i < str.length(); i++) {
    char c = str.at(i);
    if (c >= '0' && c <= '9')
    {
       num += c; // or num.push_back(c) or num.append(c);
    }
}



祝你好运。

Alex。


Good luck.
Alex.


strtol [ ^ ]



我以为还有一个strtoi,但我似乎无法找到它,也许它不是标准库的一部分。
strtol[^]

I thought there was also a strtoi, but I can't seem to find it, maybe its not part of the standard library.


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

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