加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

检查字符串是否是int golang

发布时间:2020-12-16 19:17:55 所属栏目:大数据 来源:网络整理
导读:如何在Golang中检查字符串值是否为整数? 就像是 v := "4"if isInt(v) { fmt.Println("We have an int,we can safely cast this with strconv")} 注意:我知道strconv.Atoi返回一个错误,但是有没有其他的功能呢? strconv.Atoi的问题是它将返回7为“a7” 正
如何在Golang中检查字符串值是否为整数?

就像是

v := "4"
if isInt(v) {
  fmt.Println("We have an int,we can safely cast this with strconv")
}

注意:我知道strconv.Atoi返回一个错误,但是有没有其他的功能呢?

strconv.Atoi的问题是它将返回7为“a7”

正如你所说,你可以使用strconv.Atoi为此。
if _,err := strconv.Atoi(v); err == nil {
    fmt.Printf("%q looks like a number.n",v)
}

您可以在ScanInts模式下使用scanner.Scanner(从文本/扫描仪),或使用正则表达式验证字符串,但Atoi是该作业的正确工具。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读