检查字符串是否有效 [英] Checking a string for valid date

查看:108
本文介绍了检查字符串是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的IsDate代码应该导致False,而是抛出下面的

异常。为什么?如果此功能无法正常工作,如何检查字符串是否可以转换为

日期?


Bob


代码:

Dim blnDate_Valid As Boolean = True

Dim x As String =" Hello"


blnDate_Valid = IsDate(x)


应该导致False,但抛出异常:


发生System.FormatException类型的第一次机会异常

mscorlib.dll。


附加信息:该字符串未被识别为有效日期时间。

有一个未知单词从索引0开始。


The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.

Additional information: the string was not recognized as a valid datetime.
There is a unknown word starting at index 0.


推荐答案

Dim sDate As String =" 3/1/05"

Dim dtConvDate作为日期


如果IsDate(sDate)那么

dtConvDate = sDate

结束如果

Bob Day写道:
Dim sDate As String = "3/1/05"
Dim dtConvDate As Date

If IsDate(sDate) Then
dtConvDate = sDate
End If
"Bob Day" wrote:
下面的IsDate代码应该导致False,而是抛出下面的
异常。为什么?如果此功能无法正常工作,如何检查字符串是否可以转换为
日期?

Bob

代码:
Dim blnDate_Valid As Boolean = True
Dim x As String =" Hello"

blnDate_Valid = IsDate(x)

应该导致False,但抛出异常:

在mscorlib.dll中发生System.FormatException类型的第一次机会异常。

附加信息:该字符串未被识别为有效的日期时间。
从索引0开始有一个未知单词。

The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.

Additional information: the string was not recognized as a valid datetime.
There is a unknown word starting at index 0.





如果你有IDE设置为在所有异常中断,而不仅仅是

未处理的异常然后它将在内部由异常函数抛出

,它用于确定日期是否为

有效。


更改Debug>下的IDE设置。例外情况......


HTH,


Sam


2005年3月1日星期二16 :13:27-505,Bob Day < Bo **** @ TouchTalk.net>

写道:

If you have the IDE set to break on all exceptions and not just
unhandled exceptions then it will break on the exception thrown
internally by the isDate function that it uses to determine if a date
is valid.

Change the IDE setting under Debug > Exceptions...

HTH,

Sam

On Tue, 1 Mar 2005 16:13:27 -0500, "Bob Day" <Bo****@TouchTalk.net>
wrote:
下面的IsDate代码应该导致False,而不是它抛出
以下例外。为什么?如果此功能无法正常工作,如何检查字符串是否可以转换为
日期?

Bob

代码:
Dim blnDate_Valid As Boolean = True
Dim x As String =" Hello"

blnDate_Valid = IsDate(x)

应该导致False,但抛出异常:

在mscorlib.dll中发生System.FormatException类型的第一次机会异常。

附加信息:该字符串未被识别为有效的日期时间。
从索引0开始有一个未知单词。
The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.

Additional information: the string was not recognized as a valid datetime.
There is a unknown word starting at index 0.




B-Line现在正在招聘一个华盛顿特区VB.NET

开发者为WinForms + WebServices职位。

中级到高级开发人员。对于

信息或将电子邮件简历应用于

sam_blinex_com。



B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.


Bob,


Bob Day <博**** @ TouchTalk.net> schrieb:
Bob,

"Bob Day" <Bo****@TouchTalk.net> schrieb:
下面的IsDate代码应该导致False,而是抛出下面的
异常。为什么?如果此函数无法正常工作,如何检查字符串是否可以转换为
日期?

Dim blnDate_Valid As Boolean = True
Dim x As String =" Hello"

blnDate_Valid = IsDate(x)

应该导致False,但抛出异常:

第一次机会异常在
mscorlib.dll中发生了System.FormatException类型。
The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?
[...]
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.




''IsDate''如果无法解析字符串,则会在内部抛出异常,但

此异常由''IsDate''捕获。尽管如此,如果您将IDE配置为在抛出异常时停止,则会显示例外
。你可以通过选择Debug来改变这种行为。 - > "例外..." - > Common

Language Runtime Exceptions - > 抛出异常时: - > (o)

继续。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://dotnet.mvps.org/dotnet/faqs/>



''IsDate'' internally throws an exception if the string cannot be parsed, but
this exception is caught by ''IsDate''. Nevertheless, the exception is shown
if you configured the IDE to stop whenever an exception is thrown. You can
change this behavior by choosing "Debug" -> "Exceptions..." -> "Common
Language Runtime Exceptions" -> "When the exception is thrown:" -> (o)
"Continue".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


这篇关于检查字符串是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆