在实际的工作中,需要提取程序中的字符串信息,但是程序中经常将一些数字当做字符串来进行处理,例如表盘的刻度信息,这时候就需要判断字符串是否全为数字,来进行真正意义上的字符串提取。下面介绍了判断字符串是否全为数字的方法,仅供参考。

  方法一:判断字符的ASCII范围(数字的范围为48~57)

  

 #include <iostream>
using namespace std; bool AllisNum(string str); int main( void )
{ string str1 = "wolaiqiao23";
string str2 = ""; if (AllisNum(str1))
{
cout<<"str1 is a num"<<endl;
}
else
{
cout<<"str1 is not a num"<<endl;
} if (AllisNum(str2))
{
cout<<"str2 is a num"<<endl;
}
else
{
cout<<"str2 is not a num"<<endl;
} cin.get();
return ;
} bool AllisNum(string str)
{
for (int i = ; i < str.size(); i++)
{
int tmp = (int)str[i];
if (tmp >= && tmp <= )
{
continue;
}
else
{
return false;
}
}
return true;
}

  方法二:使用C++提供的stringstream对象 

 #include <iostream>
#include <sstream>
using namespace std; bool isNum(string str); int main( void )
{
string str1 = "wolaiqiao23r";
string str2 = "";
if(isNum(str1))
{
cout << "str1 is a num" << endl;
}
else
{
cout << "str1 is not a num" << endl; }
if(isNum(str2))
{
cout<<"str2 is a num"<<endl;
}
else
{
cout<<"str2 is not a num"<<endl; } cin.get();
return ;
} bool isNum(string str)
{
stringstream sin(str);
double d;
char c;
if(!(sin >> d))
{
return false;
}
if (sin >> c)
{
return false;
}
return true;
}

  运行结果

  

C++ 判断字符串是否全是数字的更多相关文章

  1. Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字

    本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...

  2. Oracle中如何判断字符串是否全为数字

    Oracle中如何判断字符串是否全为数字 学习了:http://www.cnblogs.com/zrcoffee/archive/2012/12/11/2812744.html 本文介绍了判断字符串是 ...

  3. c#用正则表达式判断字符串是否全是数字、小数点、正负号组成 Regex reg = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");

    Regex reg = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][ ...

  4. js判断字符串是否全为空(使用trim函数/正则表达式)

    我们需要判断用户输入的是否全是空格,可以使用以下方法: 方法一: 使用trim() /* 使用String.trim()函数,来判断字符串是否全为空*/ function kongge1(test) ...

  5. java练习-判断字符串是否都是数字

    方法1: package everyDayPratise; public class IsAllNumber { public static boolean method1(String s) { i ...

  6. JavaScript判断字符串能否转化为数字

    判断一个字符串能否转化为数字 我们常常使用parseInt函数. 不过该函数的适用范围是很小的. 一般说来对于 如下类似 var myStr = "123hello"; 使用par ...

  7. js判断值是不是全是数字

    if(isNaN(value)){ 不是数字 }else{ 全是数字 }

  8. python3 之 判断字符串是否只为数字(isdigit()方法、isnumeric()方法)

    Isdigit()方法 - 检测字符串是否只由数字组成 语法:   str.isdigit() 参数: 无 返回值: 如果字符串只包含数字,则返回True,否则返回False. 实例: 以下实例展示了 ...

  9. Python判断字符串是否全是字母或数字

    str.isnumeric(): True if 只包含数字:otherwise False.注意:此函数只能用于unicode string str.isdigit(): True if 只包含数字 ...

随机推荐

  1. Windows phone应用开发[21]-图片性能优化

    在windows phone 中常在列表中会常包含比较丰富文字和图片混排数据信息. 针对列表数据中除了谈到listbox等控件自身数据虚拟化问题外.虽然wp硬件设备随着SDK 8.0 发布得到应用可使 ...

  2. ORACLE 导入导出

    数据导出:1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中exp system/manager@TEST file=d:\daochu.dmp ...

  3. easyui 汇总

    1. easyui datagrid 表格组件列属性 formatter columns:{ { field:' product', title:'商品', align:'center', width ...

  4. BZOJ4416: [Shoi2013]阶乘字符串

    可以大胆猜想n>21时无解,至于依据,不开O2,1s,n<=21刚好能卡过去= = 并不会证= = #include<cstdio> void up(int& a,in ...

  5. Java初学者笔记

    Java初学者笔记 前提:认识汉字,认识英语单词,会用键盘打字 ------------------------------- 端口是什么: --------面向对象编程(OOP) 接口”(Inter ...

  6. 工作中的一些JS--为网页动态添加元素,类似于邮箱添加联系人的功能

    项目中要解决一个为下拉框动态添加选项的问题,之前从网上搜到结果,写个JS函数 //先新建元素,并添加属性 var option = document.createElement("optio ...

  7. Jquery ui widget开发

    Jquery ui 提供了一些基本的widget,但是他提供了很好的机制来创建widget.在jquery css framework中包含了基本的css样式(视觉和感觉诸如颜色,字体大小,图标等), ...

  8. Windows远程数据同步工具cwRsync

    1. cwRsync简介cwRsync是Rsync在Windows上的实现版本,Rsync通过使用特定算法的文件传输技术,可以在网络上传输只修改了的文件.cwRsync主要用于Windows上的远程文 ...

  9. AFNetworking certificate AFNetworking 证书设置

    + (AFSecurityPolicy*)customSecurityPolicy { // /先导入证书 NSString *cerPath = [[NSBundle mainBundle] pat ...

  10. 解决COM组件80070005错误

    前段时间在维护公司以前的项目时遇到一个问题,客户需要添加一个word文档合并功能,按理说这功能比较好实现,只要调用Office自带的COM组件就搞定了,但实际上并非如此,在客户端部署上以后运行报错,提 ...