请告诉我如何在vb.net中编写下一个代码?
public static bool IsYear(this string data)
{
return data.Length == 4 && Regex.IsMatch(data, @"[12]+[0-9]+[0-9]+[0-9]");
}
答案 0 :(得分:2)
看看这个MSDN article on VB extension Methods。应用它将导致类似的事情。
Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim myString As String
myString = "1955"
Console.WriteLine(myString & " is Year =" & myString.IsYear)
myString = "3955"
Console.WriteLine(myString & " is Year =" & myString.IsYear)
myString = "19555"
Console.WriteLine(myString & " is Year =" & myString.IsYear)
Console.ReadLine()
End Sub
End Module
Module Extensions
<Extension()>
Public Function IsYear(ByVal data As String) As Boolean
Return data.Length = 4 And Regex.IsMatch(data, "[12]+[0-9]+[0-9]+[0-9]")
End Function
End Module