[英]How do i do a format check? so the input is in the correct format. E.g. dd/mm/yy. (Visual Basic)
I've long said the best way to see if a value is in the correct format is to actually try to parse it .我早就说过,查看值是否采用正确格式的最佳方法是实际尝试解析它。 This has the advantage of being a perfect validation check, instead of merely a heuristic, as well as giving you an instance of the target type to work with, which is usually the next step anyway.这具有作为完美验证检查的优点,而不仅仅是一种启发式方法,并且为您提供要使用的目标类型的实例,这通常是下一步。
Therefore I suggest DateTime.TryParseExact()
.因此我建议DateTime.TryParseExact()
。 Here's a basic example:这是一个基本示例:
Dim input As String = " ... "
Dim result As DateTime
Dim format = "dd/MM/yyyy"
If DateTime.TryParseExact(input, format, Nothing, Nothing, result) Then
' The input is in the correct format
Else
' The input is NOT in the correct format
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.