文字列を数値に変換する方法には、幾つかの方法があり、 Parse()メソッド・TryParse()メソッド・Convertクラスなどがあります。
ブール(Boolean)文字列をブール値に変換する
Public Shared Overloads Function ToBoolean(ByVal str As String) As Boolean Dim val As Boolean Try val = Boolean.Parse(str) Catch ex As System.FormatException val = False End Try Return val End Function Public Shared Overloads Function ToBoolean(ByVal obj As Object) As Boolean Return ToBoolean(obj.ToString()) End Function
TryParseを使用する方法
Dim str As String = "23456" Dim bResult As Boolean bc = Boolean.TryParse(str, bResult)
文字列を数値に変換する
'整数型Long Public Shared Overloads Function ToLong(ByVal str As String) As Long Dim val As Long Try val = Long.Parse(str) Catch ex As System.FormatException val = 0 End Try Return val End Function Public Shared Overloads Function ToLong(ByVal obj As Object) As Long Return ToLong(obj.ToString()) End Function '整数型Short Public Shared Overloads Function ToShort(ByVal str As String) As Short Return CType(ToLong(str), Short) End Function Public Shared Overloads Function ToShort(ByVal obj As Object) As Short Return CType(ToLong(obj.ToString()), Short) End Function '整数型Integer Public Shared Overloads Function ToInteger(ByVal str As String) As Integer Return CType(ToLong(str), Integer) End Function Public Shared Overloads Function ToInteger(ByVal obj As Object) As Integer Return CType(ToLong(obj.ToString()), Integer) End Function '実数型Decimal Public Shared Overloads Function ToDecimal(ByVal str As String) As Decimal Dim val As Decimal Try val = Decimal.Parse(str) Catch ex As System.FormatException val = 0 End Try Return val End Function Public Shared Overloads Function ToDecimal(ByVal obj As Object) As Decimal Return ToDecimal(obj.ToString()) End Function '実数型Double Public Shared Overloads Function ToDouble(ByVal str As String) As Double Dim val As Double Try val = Double.Parse(str) Catch ex As System.FormatException val = 0 End Try Return val End Function Public Shared Overloads Function ToDouble(ByVal obj As Object) As Double Return ToDouble(obj.ToString()) End Function
TryParseを使用する方法
Dim str As String = "23456" Dim iResult As Integer bc = Integer.TryParse(str, System.Globalization.NumberStyles.Number, _ System.Globalization.CultureInfo.InvariantCulture, iResult) Dim lResult As Long bc = Long.TryParse(str, System.Globalization.NumberStyles.Number, _ System.Globalization.CultureInfo.InvariantCulture, lResult) Dim dResult As Double bc = Double.TryParse(str, System.Globalization.NumberStyles.Any, _ System.Globalization.CultureInfo.InvariantCulture, dResult) Dim mResult As Decimal bc = Decimal.TryParse(str, System.Globalization.NumberStyles.Any, _ System.Globalization.CultureInfo.InvariantCulture, mResult)
16進数文字列の数値変換する
変換にはConvert クラスを使用します。
Public Shared Overloads Function HexStrToLong(ByVal str As String) As Long Dim val As Long Try val = Convert.ToInt64(str, 16) Catch ex As System.FormatException val = 0 End Try Return val End Function Public Shared Overloads Function HexStrToLong(ByVal obj As Object) As Long Return HexStrToLong(obj.ToString()) End Function
TryParseを使用する方法
Dim sResult As Short bc = Short.TryParse("ABCD", System.Globalization.NumberStyles.HexNumber, _ System.Globalization.CultureInfo.InvariantCulture, sResult)
Parse・ TryParseはNumberStyles列挙体を使用することにより文字列のスタイルを変えられます。
'桁の区切り記号が含まれている Dim val1 As Integer = Integer.Parse("123,456,789", _ System.Globalization.NumberStyles.AllowThousands) または Dim val1 As Integer = Integer.Parse("123,456,789", _ System.Globalization.NumberStyles.AllowThousands, _ System.Globalization.CultureInfo.InvariantCulture) '指数表記を使用する Dim val2 As Integer = Integer.Parse("123e4", _ System.Globalization.NumberStyles.AllowExponent) '通貨記号が含まれている場合 Dim val3 As Integer = Integer.Parse("\123456", _ System.Globalization.NumberStyles.AllowCurrencySymbol)
※Parse()・TryParse()の文字列の解釈はWindowsの地域設定のコントロールパネルの設定に影響されます。
・地域設定の影響を受けないようにするにはCultureInfoクラスを明示的に指定することです
・TryParse()は数値のオーバーフローのエラーは出しません。