C++ 判断字符串是否全是数字
转自:https://www.cnblogs.com/MakeView660/p/6077371.html
C++ 判断字符串是否全是数字
在实际的工作中,需要提取程序中的字符串信息,但是程序中经常将一些数字当做字符串来进行处理,例如表盘的刻度信息,这时候就需要判断字符串是否全为数字,来进行真正意义上的字符串提取。下面介绍了判断字符串是否全为数字的方法,仅供参考。
方法一:判断字符的ASCII范围(数字的范围为48~57)
1 #include <iostream>2 using namespace std; 3 4 bool AllisNum(string str); 5 6 int main( void ) 7 { 8 9 string str1 = "wolaiqiao23"; 10 string str2 = "1990"; 11 12 if (AllisNum(str1)) 13 { 14 cout<<"str1 is a num"<<endl; 15 } 16 else 17 { 18 cout<<"str1 is not a num"<<endl; 19 } 20 21 if (AllisNum(str2)) 22 { 23 cout<<"str2 is a num"<<endl; 24 } 25 else 26 { 27 cout<<"str2 is not a num"<<endl; 28 } 29 30 cin.get(); 31 return 0; 32 } 33 34 bool AllisNum(string str) 35 { 36 for (int i = 0; i < str.size(); i++) 37 { 38 int tmp = (int)str[i]; 39 if (tmp >= 48 && tmp <= 57) 40 { 41 continue; 42 } 43 else 44 { 45 return false; 46 } 47 } 48 return true; 49 }
方法二:使用C++提供的stringstream对象
1 #include <iostream>2 #include <sstream> 3 using namespace std; 4 5 bool isNum(string str); 6 7 int main( void ) 8 {9 string str1 = "wolaiqiao23r"; 10 string str2 = "1990"; 11 if(isNum(str1)) 12 { 13 cout << "str1 is a num" << endl; 14 } 15 else 16 { 17 cout << "str1 is not a num" << endl; 18 19 } 20 if(isNum(str2)) 21 { 22 cout<<"str2 is a num"<<endl; 23 } 24 else 25 { 26 cout<<"str2 is not a num"<<endl; 27 28 } 29 30 cin.get(); 31 return 0; 32 } 33 34 bool isNum(string str) 35 { 36 stringstream sin(str); 37 double d; 38 char c; 39 if(!(sin >> d)) 40 { 41 return false; 42 } 43 if (sin >> c) 44 { 45 return false; 46 } 47 return true; 48 }
运行结果:
C++ 判断字符串是否全是数字相关推荐
- Oracle中如何判断字符串是否全为数字
Oracle中如何判断字符串是否全为数字 学习了:http://www.cnblogs.com/zrcoffee/archive/2012/12/11/2812744.html 本文介绍了判断字符串是 ...
- python判断字符串是否全为数字 判断第一个字母是不是数字 isdigit
str = "123456"; print (str.isdigit())str = "Runoob example....wow!!!" print (str ...
- 正则表达式判断字符串是否全是数字、小数点、正负号组成等
"^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\d+)|(0+))$" ...
- Python判断字符串是否全是数字或者字母
一.判断为数字 str.isnumeric() Return True if all characters in the string are numeric characters, and ther ...
- js 判断字符串是否全是数字
1.可能也有小数点 var test = ['123.12', '.123', '123.', '123a']; var reg = /^[\d|\.]*$/; test.forEach((v) =& ...
- java判断数字大小写_java判断字符串是否全部由数字,大小写字母,特殊符号组成...
直接上代码,经过验证. package javaTest; import java.util.regex.Matcher; import java.util.regex.Pattern; public ...
- java判断字符串是否全部由数字,大小写字母,特殊符号组成
直接上代码,经过验证. package javaTest; import java.util.regex.Matcher; import java.util.regex.Pattern; pu ...
- 判断字符串是否由纯数字组成
package com.liaojianya.chapter2;import java.util.Scanner;/*** 判断字符串是否由纯数字组成* @author LIAO JIANYA* 20 ...
- python判断字符串是全数字或者全字母
预处理数据时,有个图像库的年龄标签可能是年龄区间,比如:(1,5).也可能是1个单个的年龄值,比如:5. 赋值时,需要判断是否为全数字:5,如果不是全数字,则需要用到分割. str.isnumeric ...
最新文章
- ndbcluster 批量修改表空间_Excel小技巧:使用这招快速保护你的工作表
- CentOS7.5安装配置PostgreSQL10
- Oracle查询会话连接数
- mysql 建立联合索引
- Java Review - 并发编程_PriorityBlockingQueue原理源码剖析
- python dlib学习(十一):眨眼检测
- ElementUI中el-time-picker实现选择时间并格式化显示和传参的格式
- 背包问题lingo求解
- 项目周期一般多久_办公家具定制的特殊性是需要一定的制作周期
- 使用Camel从WildFly 8向WebLogic 12发送JMS消息
- tar包bz2和gz的对比
- python中的数据类型中int表示_python中的基本数据类型之 int bool str
- Hadoop_17_MapRduce_案例2_实现用户手机流量统计(ReduceTask并行度控制)
- 翻译:Swift 5编写并发编程,并发解决方案和异步Operation
- Windows 10 使用Easy Sysprep V5 新版封装软件的图文封装教程
- 三组计算机局域网组网方案,多种方式组建家庭局域网
- 51单片机通过74HC595控制8位数码管,在任意位置显示数字
- java Socket IO流关闭问题(java.net.SocketException: Socket is closed)
- C#大恒相机采集图片时图片上下对称折叠了
- enumerate使用方法