判断string可否转换为int

[复制链接]
查看11 | 回复5 | 2015-10-26 13:54:29 | 显示全部楼层 |阅读模式
用正则表达式判断(下面用的是java语法):String str = "";boolean isNum = str.matches("[0-9]+"); isNum为true 则可转换 为false则不可转换...
回复

使用道具 举报

千问 | 2015-10-26 13:54:29 | 显示全部楼层
用正则表达式判断(下面用的是java语法):String str = "";boolean isNum = str.matches("[0-9]+"); isNum为true 则可转换 为false则不可转换...
回复

使用道具 举报

千问 | 2015-10-26 13:54:29 | 显示全部楼层
方法有很多种.给你一个链接,上面提供了4种方式,C#语言.如果你用的C,和这个例子也大同小异. http://www.cnblogs.com/seebbs/articles/460238.html希望对你有所帮助....
回复

使用道具 举报

千问 | 2015-10-26 13:54:29 | 显示全部楼层
否。Int32.Parse转换,不能抛出异常.用正则表达式先判断一下这个是用来表示所以 数值 类型的。 包括了浮点数bool isNumber(string str){Regex reg = new Regex(@"^\d+([.]\d+)?$");return reg.Match(str).Success;}...
回复

使用道具 举报

千问 | 2015-10-26 13:54:29 | 显示全部楼层
用try catch来实现string a="123";int i;try{i=int.Parse(a)}catch{转换错误。}...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行