文字列が数字に変換できるか調べるC#でIsNumeric、IsDateに代わるものは?文字列が数字に変換できるか調べるVBのIsNumeric関数は、指定されたデータが正しく倍精度浮動小数点数型(Double)に変換できる文字列であるか調べることができる関数です。Char構造体にはIsNumberやIsDigitというメソッドがありますが、これらは指定された文字(Char)が数字であるか調べるものであり、文字列には使えません。 C#でIsNumericの代わりになりそうな方法には、
などが考えられそうです。ここでは1と2の方法を紹介します。 Double.TryParseメソッドを使用した方法1の方法は例えば次のようになります。ここではNumberStyles.AnyとNumberFormatInfo.InvariantInfoを指定していますが、必要に応じて変更してください(詳しくは「Double.TryParse メソッド」をご覧ください)。 [VB.NET] Dim str As String = "-12.34" Dim d As Double 'doubleに変換できるか確かめる If Double.TryParse(str, _ System.Globalization.NumberStyles.Any, _ System.Globalization.NumberFormatInfo.InvariantInfo, _ d) Then Console.WriteLine("{0}は数字です。", str) Else Console.WriteLine("{0}は数字ではありません。", str) End If [C#] string str = "-12.34"; double d; //doubleに変換できるか確かめる if (double.TryParse(str, System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out d)) Console.WriteLine("{0}は数字です。", str); else Console.WriteLine("{0}は数字ではありません。", str); .NET Framework 2.0以降では、NumberStylesとIFormatProviderを指定する必要がなくなりました。さらに、Double以外の多くの型でTryParseメソッドを使用できるようになりました。 Double.Parseメソッドで変換してみて例外が発生するか調べる方法次に2番目の方法です。これは例えば次のようになります。 [VB.NET] Dim str As String = "-12.34" Dim isnum As Boolean = True 'doubleに変換できるか確かめる Try Double.Parse(str) Catch isnum = False End Try '結果を表示 If isnum Then Console.WriteLine("{0}は数字です。", str) Else Console.WriteLine("{0}は数字ではありません。", str) End If [C#] string str = "-12.34"; bool isnum = true; //doubleに変換できるか確かめる try { double.Parse(str); } catch { isnum = false; } //結果を表示 if (isnum) Console.WriteLine("{0}は数字です。", str); else Console.WriteLine("{0}は数字ではありません。", str); 文字列が日付型(DateTime)に変換できるか調べる次にIsDate関数です。VBのIsDate関数は、指定した文字列が日付型(Date)に変換可能か調べることができる関数です。IsDate関数の場合も先に示した2〜4の方法が使えます。.NET Framework 2.0以降では、1の方法も使えます。 DateTime.Parseメソッドで変換してみて例外が発生するか調べる方法次に2番目の方法を使った例を示します。 [VB.NET] Dim str As String = "2000/1/1" Dim isnum As Boolean = True 'DateTimeに変換できるか確かめる Try DateTime.Parse(str) Catch isnum = False End Try '結果を表示 If isnum Then Console.WriteLine("{0}はDateTimeに変換できます。", str) Else Console.WriteLine("{0}はDateTimeに変換できません。", str) End If [C#] string str = "2000/1/1"; bool isnum = true; //DateTimeに変換できるか確かめる try { DateTime.Parse(str); } catch { isnum = false; } //結果を表示 if (isnum) Console.WriteLine("{0}はDateTimeに変換できます。", str); else Console.WriteLine("{0}はDateTimeに変換できません。", str); DateTime.TryParseメソッドを使用した方法.NET Framework 2.0以降では、DateTime.TryParseメソッドを使用することができます。 [VB.NET] Dim str As String = "2000/1/1" Dim dt As DateTime 'DateTimeに変換できるか確かめる If DateTime.TryParse(str, dt) Then Console.WriteLine("{0}はDateTimeに変換できます。", str) Else Console.WriteLine("{0}はDateTimeに変換できません。", str) End If [C#] string str = "2000/1/1"; DateTime dt; //DateTimeに変換できるか確かめる if (DateTime.TryParse(str, out dt)) Console.WriteLine("{0}はDateTimeに変換できます。", str); else Console.WriteLine("{0}はDateTimeに変換できません。", str); DateTime.TryParseでは判断できないような特別なフォーマットの場合は、DateTime.TryParseExactメソッドを使います。DateTimeのParseとParseExactメソッドの違いと考えればよいでしょう。DateTimeのParseとParseExactメソッドに関しては、「文字列からDateTime型に変換する」をご覧ください。
|
|
Copyright 2002-2008 DOBON!. All rights reserved.
|