Asp.net

VB.NET中如何判斷字元串的第一個字元是否為數字?

  • February 5, 2022

如何檢查字元串的第一個字元是否是 VB.NET 中的數字?

我知道Java的做法是:

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

但我不確定如何為 VB.NET 做這件事。

提前感謝您的幫助。

這是一個為您提供答案的臨時程序,本質上是“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
Public Function StartsWithDigit(ByVal s As String) As Boolean
       Return (Not String.IsNullOrEmpty(s)) AndAlso Char.IsDigit(s(0))
End Function

引用自:https://stackoverflow.com/questions/1326835