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 只包含数字 ...
随机推荐
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- Servlet和JSP
Servlet 一.Servlet 的生命周期. servlet 有良好的生存期的定义,包括加载和实例化.初始化.处理请求以及服务结束.这个生存期由javax.servlet.Servlet 接口 的 ...
- Spark环境搭建遇到的问题
1.始终找不到Hive表的问题 原因:官方编译后的Spark1.2.0+hadoop2.4与hadoop2.4.1不匹配 解决方法有两个: a.将Hadoop2.4.1替换为2.4.0版本 b.重新编 ...
- 多兼容的JS获取鼠标坐标
//第一.相对于屏幕 function getMousePos(event) { var e = event || window.event; return {'x':e.screenX,'y':sc ...
- springmvc请求接收参数的几种方法
一.通过@PathVariable获取路径中的参数 @RequestMapping(value="user/{id}/{name}",method=RequestMethod.GE ...
- python学习之day5,装饰器,生成器,迭代器,json,pickle
1.装饰器 import os import time def auth(type): def timeer(func): def inner(*args,**kwargs): start = tim ...
- Java面试题总结(二)
43.Java中的两种异常类型是什么?他们有什么区别? Java中有两种异常:受检查的(checked)异常和不受检查的(unchecked)异常.不受检查的异常不需要在方法或者是构造函数上声明,就算 ...
- windows2003安装证书服务:csp配置不正确、您没有此密钥容器的写访问权限
1.填写CA名称后在生成密钥时提示:csp配置不正确或安装不完整. 原因:可能的原因为CS服务(Crysptographic Service)没有启动 . ps:该服务依赖RPC服务,但RP ...
- 消息中间件:RabbitMQ基本探索
RabbitMQ是一个基于AMQP协议(Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中 ...
- java单例的几种实现方法
java单例的几种实现方法: 方式1: public class Something { private Something() {} private static class LazyHolder ...