Hello friends,
I just ran into a strange situation.
I have a string: “v. 15. → Naniser”.
I want to extract the value 15 using a loop, since there might be multiple numbers.
The program interprets “Naniser” as NaN.
It works once I add IsNumeric.
Can you explain why?
“Nansier” is not a number but the framework parser looks for “NaN”, case insensitive at the start of the string, is my best guess. NaN does mean “Not a Number” after all, so more-or-less correct in this instance
. Do you expect to encounter “NaN”? If so then I’d do a direct case sensitive comparison for that case rather than converting.
Whether or not you know the format of the input string, you’re likely better off with RegEx, especially if this is an operation you need to perform frequently. Then you can handle multiple numeric string results within the same container string using a loop quite easily.
Untested code:
Var extractedNumbers() As String
Var rx As New RegEx
rx.SearchPattern = "\d+"
Var match As RegExMatch = rx.Search(inputString) // e.g. “v. 15. → Naniser”
While match <> Nil
extractedNumbers.Add match.SubExpressionString(0) // "15"
match = rx.Search()
End While
Just an addition: While ‘NaN’ means ‘Not a Number’ it is a technically valid value of a variable of type Double and you can assign it to such a variable. Mathematically it is an invalid value, like what you would get by calculating the square root of a negative number.
(Maybe I should get slapped on my wrist for this but I have sometimes used a sqrt(-1) value to indicate that the actual value isn’t currently known.)
Many thanks for the explanations and the code as well!
What I don’t understand is that my variable is declared as a string.
And thanks also for teaching me about RegEx.
Can you show us your code?
Dim NumS(-1) As String
if NumS(i) = "01." OR NumS(i) = "02." OR ( Val(NumS(i)) >= 10 AND Val(NumS(i)) <= 210 ) Then
Num_Conjug.Append( Val(NumS(i)) ) // On mémorise les nombres.
end if
As it stands this code snippet doesn’t make much sense. Where is the value of the variable i coming from? Did you forget to include a For loop?
You start out with an empty string array but in the second line you assume it somehow got filled – NumS(i) would raise an error if NumS was still empty.
That NumS is declared as an array of strings does make sense though.
In the age of AI, I hope my humble human intelligence at least had the sense to increment i.
The reason I’m asking is that, out of 69,000 records, I ran into this problem and only with naniser.
Of course, I didn’t include all the code.
That’s the problem … With just some random lines of code to go on I don’t understand what “What I don’t understand is that my variable is declared as a string” even means. (The obvious answer would be “Because you have declared it as such”.)
Take a chain “v. 15. → Naniser”
You split it in order to get Nums(i) and tell me what it happens with the word “Naniser”.