C#中最简单验证数据是否为日期或数字的方法是什么? VB.Net中可以用isNumeric,isDate,但C#中没有。除了正则式和 parse+异常外,还有什么好的方法吗?当然了,不要说自己写函数哦:) 另外,正则和异常判断这两种方法应该是正则式强一些的吧 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 VB.NET中保留了旧函数,是为了照顾以前老VB用户,但内部实现已经变了,而且并不符合.NET面向对象的思想;应尽量用.NET Framework的方法~ 使用2.0 调用TryParse()1.xint num = 0;double result;bool parseResult = double.TryParse("123", out result)if( parseResult && result<= int.MaxValue && result >= int.MinValue ){//..........}else{//..........} DateTime 转换只能使用Try catch或是正则表达式了 ofei(欧卡):2.0有TryParse,可惜1.1没有啊难道只有正则或异常了吗?天哪,判断一个数据,居然要写一堆try catch,而且异常捕捉时速度贼慢 if(Microsoft.VisualBasic.Information.IsNumeric(this.txt.Text)==false) cansum396(沉觉不醒) :这个实在是……太霸道了 反编译Microsoft.VisualBssic中的函数到C#:v1.1.4322public static bool IsDate(object Expression){ if (Expression != null) { if (Expression is DateTime) { return true; } if (Expression is string) { try { DateTime time1 = DateType.FromString((string) Expression); return true; } catch (Exception) { } } } return false;}public static bool IsNumeric(object Expression){ IConvertible convertible1 = null; double num1; if (Expression is IConvertible) { convertible1 = (IConvertible) Expression; } if (convertible1 == null) { if (Expression is char[]) { Expression = new string((char[]) Expression); } else { return false; } } TypeCode code1 = convertible1.GetTypeCode(); if ((code1 != TypeCode.String) && (code1 != TypeCode.Char)) { return Utils.IsNumericTypeCode(code1); } string text1 = convertible1.ToString(null); try { long num2; if (StringType.IsHexOrOctValue(text1, ref num2)) { return true; } } catch (Exception) { return false; } return DoubleType.TryParse(text1, ref num1);} v2.0.50727public static bool IsDate(object Expression){ if (Expression != null) { if (Expression is DateTime) { return true; } string text1 = Expression as string; if (text1 != null) { DateTime time1; return Conversions.TryParseDate(text1, ref time1); } } return false;} public static bool IsNumeric(object Expression){ double num1; IConvertible convertible1 = Expression as IConvertible; if (convertible1 == null) { char[] chArray1 = Expression as char[]; if (chArray1 != null) { Expression = new string(chArray1); } else { return false; } } TypeCode code1 = convertible1.GetTypeCode(); if ((code1 != TypeCode.String) && (code1 != TypeCode.Char)) { return Information.IsOldNumericTypeCode(code1); } string text1 = convertible1.ToString(null); try { long num2; if (Utils.IsHexOrOctValue(text1, ref num2)) { return true; } } catch (StackOverflowException exception1) { throw exception1; } catch (OutOfMemoryException exception2) { throw exception2; } catch (ThreadAbortException exception3) { throw exception3; } catch (Exception) { return false; } return DoubleType.TryParse(text1, ref num1);} 有错字Microsoft.VisualBssic => Microsoft.VisualBasic 1.1中用异常2.0中用TryParseDate我觉得还是foreach遍历,自己分析比较好日期主要是分析日是否超过月最大天数数字更简单,Char.IsDigit判断就可以了 viena(维也纳nn木人 [IQ=50,EQ<0]) 的东东强 c#中读取数据库内时间,然后显示出来! 如何用C#实现网络编程 datagridview 单元格联动 C#父窗体 引用添加 几个子窗体 搞定就给分了! 请问在PDA里如何用C#获取桌面的句柄 sendmessags,单击问题 WEB引用出显看不懂 C#中trayicon中怎么使用OwnerDraw的menu? 求win2003中asp.net2.0部署环境要求(100分在线等!) xp home版 如何玩web 呀 winform中怎样用listbox邦定DataSet? 为什么这个TIMER报这样的错呢?
int num = 0;
double result;
bool parseResult = double.TryParse("123", out result)
if( parseResult && result<= int.MaxValue && result >= int.MinValue )
{
//..........
}
else
{
//..........
}
天哪,判断一个数据,居然要写一堆try catch,而且异常捕捉时速度贼慢
v1.1.4322
public static bool IsDate(object Expression)
{
if (Expression != null)
{
if (Expression is DateTime)
{
return true;
}
if (Expression is string)
{
try
{
DateTime time1 = DateType.FromString((string) Expression);
return true;
}
catch (Exception)
{
}
}
}
return false;
}
public static bool IsNumeric(object Expression)
{
IConvertible convertible1 = null;
double num1;
if (Expression is IConvertible)
{
convertible1 = (IConvertible) Expression;
}
if (convertible1 == null)
{
if (Expression is char[])
{
Expression = new string((char[]) Expression);
}
else
{
return false;
}
}
TypeCode code1 = convertible1.GetTypeCode();
if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
{
return Utils.IsNumericTypeCode(code1);
}
string text1 = convertible1.ToString(null);
try
{
long num2;
if (StringType.IsHexOrOctValue(text1, ref num2))
{
return true;
}
}
catch (Exception)
{
return false;
}
return DoubleType.TryParse(text1, ref num1);
}
v2.0.50727public static bool IsDate(object Expression)
{
if (Expression != null)
{
if (Expression is DateTime)
{
return true;
}
string text1 = Expression as string;
if (text1 != null)
{
DateTime time1;
return Conversions.TryParseDate(text1, ref time1);
}
}
return false;
}
public static bool IsNumeric(object Expression)
{
double num1;
IConvertible convertible1 = Expression as IConvertible;
if (convertible1 == null)
{
char[] chArray1 = Expression as char[];
if (chArray1 != null)
{
Expression = new string(chArray1);
}
else
{
return false;
}
}
TypeCode code1 = convertible1.GetTypeCode();
if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
{
return Information.IsOldNumericTypeCode(code1);
}
string text1 = convertible1.ToString(null);
try
{
long num2;
if (Utils.IsHexOrOctValue(text1, ref num2))
{
return true;
}
}
catch (StackOverflowException exception1)
{
throw exception1;
}
catch (OutOfMemoryException exception2)
{
throw exception2;
}
catch (ThreadAbortException exception3)
{
throw exception3;
}
catch (Exception)
{
return false;
}
return DoubleType.TryParse(text1, ref num1);
}
Microsoft.VisualBssic => Microsoft.VisualBasic
2.0中用TryParseDate我觉得还是foreach遍历,自己分析比较好
日期主要是分析日是否超过月最大天数
数字更简单,Char.IsDigit判断就可以了