c#:字符串转int问题

itlao6 开发&源码 .NET 原创评论784字数 257阅读0分51秒阅读模式
类型转换是初学者的常见问题,刚接触c#的我也不例外,所幸有其他语言的基础,猜想会有一些容错率较高的api…

于是,输入“int.”,果然,字符串转int可以通过下面方法实现,在转换的同时,做了错误捕获,当不能转换时,返回false,并将转换后的值置为0文章源自IT老刘-https://itlao6.com/801.html

var iStr = "987";文章源自IT老刘-https://itlao6.com/801.html

int iInt; int.TryParse(iStr, out iInt);文章源自IT老刘-https://itlao6.com/801.html

上述方法转换成功返回true,iInt值为转换后的987; 如果字符串不能转数字,转换失败返回false,iInt值为0文章源自IT老刘-https://itlao6.com/801.html

原文:简书ThinkinLiu 博客: IT老五文章源自IT老刘-https://itlao6.com/801.html 文章源自IT老刘-https://itlao6.com/801.html

weinxin
我的微信公众号
微信扫一扫关注公众号,不定时更新
itlao6
  • 本文由 发表于 2019年 3月 2日 19:06:00
  • 转载请务必保留本文链接:https://itlao6.com/801.html
评论  0  访客  0
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定