There are still many useful things in VB. NET. For example, VB. NET can directly call various useful functions in the Microsoft. VisualBasic namespace, especially some type verification functions (isdate, isnumeric. To determine whether it is a number, I found three methods on the Internet used in C #.(But each has its own shortcomings):
1. Break the string into char and use Char. isnumber (c) for verification.
Public Bool Isnumericchar ( String Str)
{
If (Str = Null | Str. Length = 0 )
{
Return False;
}
Foreach ( Char C In Str)
{
If ( ! Char. isnumber (c ))
{
Return False;
}
}
Return True ;
}
Disadvantages:Only positive integer numbers can be verified. It is invalid for floating-point numbers and negative numbers.
2. Use a regular expression to determine whether a number is used.
Public Bool Isnumericregex ( String Str)
{
System. Text. regularexpressions. RegEx Reg= NewSystem. Text. regularexpressions. RegEx (@"^ [-]? \ D + [.]? \ D * $");
ReturnReg. ismatch (STR );
}
Disadvantages:There are various types of numbers, such as the scientific notation, which cannot be judged, for example, 1.234568e + 008. Of course, you can consider modifying the regular expression, but it is not clear what form we have not considered.
3. Use try... catch... to convert the specified string to a number. If the conversion fails, the string is not a number.
Public Bool Isnumerictry ( String Str)
{
Try
{
//First judge whether it is an integer
Int32.parse (STR );
}
Catch
{
// Determines whether it is a floating point
Try
{
Double. parse (STR );
}
Catch
{
Return False;
}
}
Return True ;
}
Disadvantages:But the performance is compromised. (Someone tested it, but I didn't test it)
Note: The following methods are stupid. Please refer to the description and comments in the last section. This section is retained to serve as a warning.
Why don't we use Microsoft. VisualBasic. isnumeric (OBJ) functions provided by Microsoft? You cannot directly call functions in the Microsoft. VisualBasic namespace in C #, but you can consider creating a VB. Net project and then referencing it in C. The method is as follows:
1. Create a VB. Net project and add a verification function class named validator.
Namespace vbutilities Namespace Vbutilities
Public Class validator Class Validator
Public Shared Function isnumeric () Function Isnumeric ( Byval OBJ As Object ) As Boolean
Return Microsoft. VisualBasic. Isnumeric (OBJ)
End Function
End Class
End namespace
2. ReferenceProgramSet. Use the validator. isnumeric (OBJ) method to call it.
Using Functions in the Microsoft. VisualBasic namespace, we can also implement more verification functions, such as verifying whether it is a date type (using isdate ). This saves a lot of trouble to write it by yourself. Are there other methods? Share some of them together!
Note:Through the comments in this article, I have some knowledge about the Visual Basic module and static import. in C #, I directly reference Microsoft. visualBasic. DLL, and then call Information. like isnumeric, it can be judged that my original method is indeed a bit stupid.
Another sentence:WriteArticleOne is to deepen your understanding of knowledge, the other is to let others point out their misunderstanding of knowledge, and the third is to communicate. The blog garden provides a good environment for everyone to maintain.