I would like to know how I would check of the third digit in a number is a 0 or not...
so i can say......if the third digit is 0 then do domething......
if xxxx(120456789) = 0 then do this....
rgs
TonyWell you can do this a number of ways. You could try this one.
Dim sNumbers as String = "124509"
Dim x as Int32
x = sNumbers.IndexOf("0")
If x = 2 Then
'0 is the third digit
End If
Of course, if the "string" of integers is of type Integer, then you'll need to convert it to a string first.
An interesting way (assuming your are looking RIGHT to LEFT):
Function GetDigit( ByVal lNumber As Long, ByVal intDigit As Integer ) As IntegerNo Math way (assuming LEFT to RIGHT):If intDigit < 1 Then
Return -1 ' invalid request
End If' lNumber = 4324
' intDigit = 3' lNumber /= 10^2 (100)
' lNumber = 43
' 43 Mod 10 = 3lNumber /= Math.Pow(10, intDigit - 1)
Return CInt(lNumber Mod 10)
End Function
Function GetDigit( ByVal lNumber As Long, ByVal intDigit As Integer ) As IntegerIf intDigit < 1 Then
Return -1 ' invalid request
End IfDim strNumber As String = lNumber.ToString()
If strNumber.Length >= intDigit Then
Return CInt(Char.GetNumericValue(strNumber.Chars(intDigit - 1))) ' For RIGHT to LEFT: change "intDigit - 1" to "strNumber.Length - intDigit - 1"
Else
' invalid request
Return -1
End IfEnd Function
Frank, what if the number is "123400" ?
Then x = 4, and also 5 if you look further.
gents.,,.....
my number can and will look like these
900000001
980654021
900004321
984000321
980000321
will mid work
dim my var as integer = 900000001
if mid(myvar, 1,"1") = 9 then the above is true
if mid(myvar, 1,"0") = 0then the above is true
may need a long here...
many thanks...
Use my second function example and simply plug in the number:
If GetDigit(900000001, 1) = 9 ThenObviously, you can replace the actual number with a variable.
' The first digit is 9
End IfIf GetDigit(900000001, 3) = 0 Then
' The third digit is 0
End If
pickyh3d:
But wasn't he just looking for "0" in the third index? So wouldn't IndexOf bring back the index of the first occurence? So if the string is what you provided: "123400", then the index it would return would be 4. Therefore we know that 0 IS NOT in the third position.
That's where I was going with my example, but maybe I misunderstood...?
Well, I was leaning towards more of the problematic approach and I showed the numbers backwards (because initially I was thinking read from right-to-left).
Imagine this number then:
103000
IndexOf('0') would return 1, which would require another search.
how about...
"103000".IndexOf("0",2)
lets not forget that IndexOf has an overload that takes a starting position
Gotcha! I didn't give that one enough thought, and that would definitely be a pitfall.
mbanavige:
Good call, that looks like a solid solution. Now you're just looking at the third position, which makes more sense.
I have to admit, i'm partial to the substring solution.
although we can get IndexOf to start at the 3rd position, it will happily search till it hits the end of the string which is still more work that is required for the solution. Also, it will throw an error if we try to start it past the end of the string so additional error handling is required.
Ya, I was thinking about the [second param] starting position the whole time, but well, I think we are in agreement at this point.
0 comments:
Post a Comment