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

  方法一:判断字符的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. 【jQuery示例】遍历表单数据并显示

    <!DOCTYPE html> <html> <head> <style> body, select { font-size:14px; } form ...

  2. Sniffer的完整代码,基于winpcap抓包统计吞吐量

    using System; using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation; using ...

  3. Oracle11g字符集AL32UTF8修改为ZHS16GBK详解【转】

    ------感谢作者,确实解决了问题.分享下,希望帮到更多人 此问题发生在数据库迁移过程中.源数据库:自己笔记本上win7 64位系统的oracle11g个人版,字符集ZHS16GBK :目标数据库, ...

  4. [转]Java compiler level does not match解决方法

    查看链接:http://jingyan.baidu.com/article/95c9d20da3ec5fec4e756186.html

  5. 为开发者准备的 Android 函数库(2016 年版)

    转载:http://www.androidchina.net/5922.html第三方函数库(译者注:包括第三方提供的 SDK,开源函数库)以惊人的方式助力着 Android 开发,借助这些其他开发人 ...

  6. npm命令教程

    教程:http://www.runoob.com/nodejs/nodejs-npm.html 常用命令:http://www.cnblogs.com/PeunZhang/p/5553574.html

  7. RFID-RC522、FM1702SL、M1卡初探

    catalogue . 引言 . RC522芯片(读卡器)简介 . FM1702SL芯片(读卡器)简介 . RFID M1卡简介 . 读取ID/序列号(arduino uno.MFRC522芯片 Ba ...

  8. 项目中遇到的关于兄弟controller之间传值的问题解决

    层级关系如下 <ons-page ng-controller="tabbarIndexController"> <ons-tabbar position=&quo ...

  9. TypeScript Type Compatibility(类型兼容)

    TypeScript中的类型兼容是基于结构归类的.在普通分类的相比之下,结构归类是一种纯粹用于将其成员的类型进行关联的方法.思考下面的代码: interface Named { name: strin ...

  10. matlab 有趣小细节

    图像的默认显示方式,坐标从1开始计数.是从左向右,从上到下为正,分别为x和y轴          如果加入x轴和y轴画成成的格子,起始位置并不是严格的左上角,而是像素的中心点,并不是以像素的边缘画格子 ...