littleisharp Posted April 23, 2004 Posted April 23, 2004 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 Quote
wessamzeidan Posted April 23, 2004 Posted April 23, 2004 Char.IsDigit() checks if a character is a digit Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
Administrators PlausiblyDamp Posted April 23, 2004 Administrators Posted April 23, 2004 You could use regular expressions try something like Function IsNumber(ByVal test As String) As Boolean If System.Text.RegularExpressions.Regex.IsMatch(test, "^\d*$") Then Return True Else Return False End If End Function Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mocella Posted April 23, 2004 Posted April 23, 2004 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] Quote
mocella Posted April 23, 2004 Posted April 23, 2004 Damn - PlausiblyDamp beat me to the punch, and with a different approach! Quote
littleisharp Posted April 26, 2004 Author Posted April 26, 2004 Hi guys I found something in the framework after all... Microsoft.VisualBasic.Information.IsNumeric(tableName) works perfectly Thanks Quote
Administrators PlausiblyDamp Posted April 26, 2004 Administrators Posted April 26, 2004 That will only work in VB - if you need to convert to C# that will fail. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jaco Posted April 26, 2004 Posted April 26, 2004 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. Quote
mocella Posted April 26, 2004 Posted April 26, 2004 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] Quote
Arch4ngel Posted April 26, 2004 Posted April 26, 2004 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. Quote "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
jspencer Posted April 26, 2004 Posted April 26, 2004 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 Quote
Leaders dynamic_sysop Posted April 26, 2004 Leaders Posted April 26, 2004 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; } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.