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 只包含数字 ...
随机推荐
- 关于 MonoDevelop on Linux 单步调试问题的解决
在 MonoDevelop 中默认是关闭对外部程序集(.dll)的调试,可通过如下步骤来解决这个问题. 通过菜单[Edit]-[Preferences]-[Debugger]进入到调试器的设置页,把“ ...
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
string.IsNullOrEmpty 都知道,这个功能是判断字符串是否为:null或者string.Empty.如果是如"\t"这样的字符就返回false了,为了达到判断过滤这 ...
- 基于NFS的分布式持久化
基于容器的微服务架构中,分布式持久化方案并没有一个默认的最好方案,这里使用NFS来作为容器持久化方案. NFS服务需要在服务器及需要挂载的客户端上分别安装配置. nfs-utils包含服务: rpcb ...
- Angular自定义指令(directive)
angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...
- js获取Html元素的实际宽度高度
第一种情况就是宽高都写在样式表里,就比如#div1{width:120px;}.这中情况通过#div1.style.width拿不到宽度,而通过#div1.offsetWidth才可以获取到宽度. 第 ...
- git Bash常用命令
1.Construct ssh key (If you want to commit to git server via THIS COMPUTER) git config --global user ...
- Git for Windows - The Program can't start because libiconv2.dll is missing
今天在新装的win10 预览版上面,发现git不能启动了,提示信息主要是: The Program can't start because libiconv2.dll is missing 于是我在网 ...
- C#中的多态性
1.重载(overload) public void Sleep() { Console.WriteLine("Animal睡觉"); } public int Sleep(int ...
- 初学Java 精简知识点总结
面对Java丰富的知识资料,很多初学者难免觉得迷惘,该学什么,怎么去学?下面给大家讲Java基础知识做了精简总结,来帮助你梳理学习思路,赶快看看吧! 方法/步骤1 对象的初始化(1) 非静态对象的初始 ...
- code::blocks编译出错
问题描述: 在windows xp 上编译的cbp项目(已经生成.obj文件),放到fedora上无法顺利编译.(build) collect2:error: ld returned 1 exit s ...