代码之家  ›  专栏  ›  技术社区  ›  LiamGu

如何确定字符串的第一个字符是否是vb.net中的数字?

  •  5
  • LiamGu  · 技术社区  · 15 年前

    如何检查字符串的第一个字符是否是vb.net中的数字?

    我知道Java的方式是:

    char c = string.charAt(0);
    isDigit = (c >= '0' && c <= '9');
    

    但我不确定如何在vb.net上实现它。

    事先谢谢你的帮助。

    5 回复  |  直到 7 年前
        1
  •  5
  •   Rob    15 年前

    下面是一个提供答案的草稿程序,本质上是“isNumeric”函数:

    Sub Main()
        Dim sValue As String = "1Abc"
        Dim sValueAsArray = sValue.ToCharArray()
        If IsNumeric(sValueAsArray(0)) Then
            Console.WriteLine("First character is numeric")
        Else
            Console.WriteLine("First character is not numeric")
        End If
    
        Console.ReadLine()
    End Sub
    
        2
  •  6
  •   Marcus Andrén    15 年前
    Public Function StartsWithDigit(ByVal s As String) As Boolean
            Return (Not String.IsNullOrEmpty(s)) AndAlso Char.IsDigit(s(0))
    End Function
    
        3
  •  0
  •   Community T.Woody    7 年前

    如果我是你,我会用 dim bisnumer=isNumeric(sValue.SubString(0,1))。 而不是 dim sValueAsArray=sValue.ToCharArray()。

    不管你用什么,都会产生相同的结果, 但说了这句话; dim sValueAsArray=sValue.ToCharArray()。 将使用更多内存 dim bisnumer=isNumeric(sValue.SubString(0,1))。 将使用更少的资源。虽然两者都可以忽略不计

    它更多的是一个编程实践的建议,而不是其他任何东西。

        4
  •  0
  •   GSerg    7 年前
    Public Function StartsWithDigit(ByVal s As String) As Boolean
        Return s Like "#*"
    End Function