代码之家  ›  专栏  ›  技术社区  ›  Booji Boy

在VB.NET中测试字符串是否可以转换为整数

  •  12
  • Booji Boy  · 技术社区  · 17 年前

    有没有更好的方法来测试字符串是否可以转换为整数,而不是像下面这样的东西?

    Public Function IsInt(ByVal value As Object) As Boolean
        Try
            Dim temp As Integer = CInt(value)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    

    我所说的“更好”是指更少的冗长和/或没有例外。

    谢谢你。

    似乎MarkJ是正确的,上面的函数似乎与IsNumeric相同,所以我想这就是我的答案。我不知道为什么我认为CInt比IsNumeric更严格。我想最好使用CInt verses is Numeric进行测试,因为这是我用来进行转换的函数?

    11 回复  |  直到 17 年前
        1
  •  16
  •   MarkJ    17 年前

    您可以使用内置的 IsNumeric

    Dim CanConvert as Boolean = IsNumeric(value)
    

    http://msdn.microsoft.com/en-us/library/6cd3f6w1(VS.71).aspx

        2
  •  6
  •   John M Gant aman_novice    17 年前

    isn't supported 在紧凑的框架上,regex是您的下一个最佳选择。

    Regex.IsMatch(value, "^-?\d+$")
    Regex.IsMatch(value, "^-?\d+(?:\.\d+)?$")
    

    如果你需要考虑科学记数法,你需要稍微调整一下。其实没那么糟糕。你已经找到了弦的开头 ^ ,可选破折号 -? ,一个或多个数字 \d+ (?:) 它查找一个小数点 \. \d+ ? 允许非捕获组的零个或一个实例,然后是字符串的结尾 $ .

    编辑: \d+ ,你可以 \d{1,8} 例如

        3
  •  5
  •   Joel Coehoorn    17 年前
    Public Function IsInt(ByVal value As Object) As Boolean
        Dim i As Integer
        Return Integer.TryParse(Convert.ToString(value), i)
    End Function
    
        4
  •  5
  •   Grizzly    17 年前

    Integer.TryParse ,它将返回一个bool,指示转换是否成功

        5
  •  3
  •   Brienne Schroth    17 年前

    如果您想避免使用异常,您可以将其与一个在转换之前只允许数字字符的正则表达式进行匹配。

        6
  •  2
  •   Michael Burr    17 年前

    TryParse() 这不会影响性能。

    如果您要执行数百万次转换,并且其中大量转换可能失败,那么您捕获的异常可能是性能问题(可能)。

    否则,将字符串解析为int,这将在99%的情况下成功。只有当它不合适时,你才会得到一个例外。如果您真的想避免Parse()可能生成的异常,那么您自己实际解析数字的sting并不困难,如果超出范围则返回failure。

    胰蛋白酶() :

        7
  •  1
  •   Shawn    17 年前
    Dim s as String = "23"
    Dim i as Integer
    If Int32.TryParse(s, i) Then
        ' String was a valid integer... '
    End If
    
        8
  •  1
  •   CraigTP    17 年前

    使用 TryParse

    Private Function CanStringBeCastAsInteger(ByVal myString As String) As Boolean
        Dim myInt As Integer = 0
        Return Integer.TryParse(myString, myInt)
    End Function
    

    使用TryParse方法的好处是,它避免了在强制转换失败时必须抛出并随后捕获异常。抛出和捕获异常是一项昂贵的操作。

    TryParse方法不仅会返回一个True/False结果,告诉您转换是否成功,它还会在我的示例中的myInt参数中为您返回结果转换,全部在一行代码中。

        9
  •  1
  •   DevByDefault    17 年前

    这里有一个非常类似于您已经拥有的东西,但是使用Convert类而不是CType,并且不使用TryParse

        Public Function IsInt(ByVal value As Object) As Boolean
        Try
            Convert.ToInt32(value)
            Return True
        Catch ex As System.FormatException
            Return False
        End Try
    End Function
    
        10
  •  0
  •   Hasani Blackwell    17 年前

    您需要滚动自己的regex e.x.:regex.IsMatch(“4354354”,“\d+”),并且仍然包括try/catch块作为备份。

        11
  •  -1
  •   Dhia Chase Isley    10 年前

    照办

        If myString <> "" And IsNumeric(myString) = True Then
    
            If CDbl(myString) - Int(CDbl(myString)) > 0 Then
                 .... myString is integer
            End If
    
        End If