C++ 判断字符串是否全是数字
在实际的工作中,需要提取程序中的字符串信息,但是程序中经常将一些数字当做字符串来进行处理,例如表盘的刻度信息,这时候就需要判断字符串是否全为数字,来进行真正意义上的字符串提取。下面介绍了判断字符串是否全为数字的方法,仅供参考。
方法一:判断字符的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++ 判断字符串是否全是数字的更多相关文章
- Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字
本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...
- Oracle中如何判断字符串是否全为数字
Oracle中如何判断字符串是否全为数字 学习了:http://www.cnblogs.com/zrcoffee/archive/2012/12/11/2812744.html 本文介绍了判断字符串是 ...
- 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][ ...
- js判断字符串是否全为空(使用trim函数/正则表达式)
我们需要判断用户输入的是否全是空格,可以使用以下方法: 方法一: 使用trim() /* 使用String.trim()函数,来判断字符串是否全为空*/ function kongge1(test) ...
- java练习-判断字符串是否都是数字
方法1: package everyDayPratise; public class IsAllNumber { public static boolean method1(String s) { i ...
- JavaScript判断字符串能否转化为数字
判断一个字符串能否转化为数字 我们常常使用parseInt函数. 不过该函数的适用范围是很小的. 一般说来对于 如下类似 var myStr = "123hello"; 使用par ...
- js判断值是不是全是数字
if(isNaN(value)){ 不是数字 }else{ 全是数字 }
- python3 之 判断字符串是否只为数字(isdigit()方法、isnumeric()方法)
Isdigit()方法 - 检测字符串是否只由数字组成 语法: str.isdigit() 参数: 无 返回值: 如果字符串只包含数字,则返回True,否则返回False. 实例: 以下实例展示了 ...
- Python判断字符串是否全是字母或数字
str.isnumeric(): True if 只包含数字:otherwise False.注意:此函数只能用于unicode string str.isdigit(): True if 只包含数字 ...
随机推荐
- Android 自定义Dialog类,并在Activity中实现按钮监听。
实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...
- ECharts学习(1)--简单图表的绘制
1.获取ECharts 官网 下载:http://echarts.baidu.com/download.html 2.在html页面中引入ECharts文件 <!DOCTYPE html> ...
- AudioRecord类获取录音音量分贝数
转自:http://www.jb51.net/article/64806.htm public class AudioRecordDemo { private static final S ...
- 关于LuCi
好吧,又长见识了...相见恨晚的赶脚,恩,居然是我喜欢的lua.其主页在这里:http://luci.subsignal.org/ The initial reason for this projec ...
- POJ 3349 Snowflake Snow Snowflakes(简单哈希)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 39324 Accep ...
- UICollectionView
#import "ViewController.h" @interfaceViewController ()<UICollectionViewDataSource,UICol ...
- Instant Radiosity实现
本来说等把课程作业做完再来弄这个,但是还是没有忍住,先做了,主要原因还是这个算法很容易实现.这个算法在1997年由Keller首次提出.虽然名字叫Instant Radiosity,但是它和Radio ...
- Bash 为何要发明 shopt 命令
在 Bash 中,有两个内置命令用来控制 Bash 的各种可配置行为的开关(打开或关闭),这些开关称之为选项(option).其中一个命令是 set,set 命令有三种功能:显示所有的变量和函数:修改 ...
- Bash 中 SHLVL 变量为 1000 的时候
SHLVL 环境变量代表 Shell 嵌套执行的深度. $ echo $SHLVL 1 $ bash $ echo $SHLVL 2 $ bash $ echo $SHLVL 3 在 Bash 里,这 ...
- PHP 连接 MySQL
PHP 连接 MySQL PHP 5 及以上版本建议使用以下方式连接 MySQL : MySQLi extension ("i" 意为 improved) PDO (PHP Dat ...