Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

What's the best and most elegant way to test whether a value in a string is numeric? Is there some function in the framework that does it?

 

My solution below is a bit snide:

 


        ' Check if it's a number or a string
       Dim i As Integer
       Dim isNumber As Boolean = True
       For i = 1 To tableName.Length()
           Select Case Microsoft.VisualBasic.Strings.Asc(Strings.Mid(tableName, i, 1))
               Case 48 To 57
                   ' This is a number
               Case Else
                   isNumber = False
                   Exit For
           End Select
       Next i

      If isNumber Then
                    ' If it's a number, enclose in single quotes 
       End If

Posted

You could do a regular-expression. I think that using "[0-9]" should tell you if there's numbers in the string. Something like this should work (although there's certainly other approaches that will do the same):

 


Regex expression = new Regex( "[0-9]" );

//perform the search in a match-collection object
MatchCollection matches = expression.Matches( tableName );

if( matches.Count > 0 )
{
isNumber = true;
}
[/Code]

Posted
That will only work in VB - if you need to convert to C# that will fail.

 

No, the Microsoft.VisualBasic namespace is a core part of the framework and will work from C# even if VB is not installed. I'm calling this namespace's methods for a few things I can't be bothered to reinvent.

Posted

I hope this isn't one of the "can't be bothered to reinvent" items (perhaps you meant some other functions in the VisualBasic namespace though).

 

I don't see having to write one common function to check a string for a given RegEx as a bad thing if you implement it correctly. Once you have a general function written, you only need to update the expression sent in, along with the string to search.

 

For example (forgive the spacing):


public static bool SearchStringForExpression( string textToSearch, string searchExpression )
{

bool expressionFound = false;
Regex expression = new Regex( searchExpression );

//perform the search in a match-collection object
MatchCollection matches = expression.Matches( textToSearch );

if( matches.Count > 0 )
{
expressionFound = true;
}
else
{
expressionFound = false;
}

return expressionFound;

}
[/Code]

Posted

int.Parse("125 Miles per hours"); couldn't work also ? Just check if != "".

 

RegEx is powerful however... might lead to much larger result than this simple eg.

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

This is the one that works for me:

 

    Public Overloads Function IsNumeric(ByVal Value As String) As Boolean
       Dim myRegEx As New Regex("(\+|-)?[0-9][0-9]*(\.[0-9]*)?")
       If myRegEx.IsMatch(Value) AndAlso myRegEx.Match(Value).Length = Value.Length Then
           Return True
       Else
           Return False
       End If
   End Function

  • Leaders
Posted

on the last post , what would happen if the number contained an E after the dot , eg: 1.E560 , it wouldn't be recognized as a numeric.

here's an example i made a while ago ( i posted this in the codebank of another vb forum when i made it )

private bool IsNumeric(object ValueToCheck)
{
double Dummy = new double();
string InputValue = Convert.ToString(ValueToCheck);

bool Numeric = double.TryParse( InputValue , System.Globalization.NumberStyles.Any , null , out Dummy);
	
return Numeric;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...