验证短范围内的字符串 [英] Validate string within short range

查看:132
本文介绍了验证短范围内的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字符串中有一个短整数。例如:123456。是否有任何API检查字符串是否包含无符号短整数范围内的有效数字?



谢谢!

  istringstream istr( 12346); 
short s;
if((istr>> s)和istr.eof())
cout< valid:<< s<< endl;
else
cout<< invalid<< endl;

(需要标题 sstream 。)


I have a short integer in a string. For Eg: "123456". Is there any API to check whether the string contains a valid number in the range of unsigned short?

Thanks!

解决方案

Simply use the stream operators to input the number:

istringstream istr("12346");
short s;
if ((istr >> s) and istr.eof())
    cout << "valid: " << s << endl;
else
    cout << "invalid" << endl;

(Needs the header sstream.)

这篇关于验证短范围内的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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