So I poached some code from another program I have and I am trying to figure out how to get it to see two decimals in a string.
Example: 10.2.1
Example: 0.12.2
Example: 0.0.18
Here is the base code I have to work with… I can find the first decimal… but haven’t figured out how to find the second one.
' ---- Variable Defined ----
Var rsData As RowSet = modSel.mthGeneralSelect( "tblChgLog", "DESC" )
Var sIter As String = rsData.Column( "chgIter" ).StringValue
Var iMaj, iMin, iWee, i As Integer
Var strItem As String
' ---- Code In Action ----
i = sIter.IndexOfBytes( "." ) // Find the decimal
If( i = -1 ) Then
If( sIter = "" ) Then
strItem = "No Data Found"
Else
strItem = ""
End If
Else
strItem = ""
End If
rsData.Close
I want to put each number value into a variable for a task, ignoring the decimal.
Example: 10.2.1 iMaj = 10 iMin = 2 iWee = 1
Example: 0.12.2 iMaj = 0 iMin = 12 iWee = 2
Example: 0.0.18 iMaj = 0 iMin = 0 iWee = 18
var Data as string = "10.2.1"
var iMaj as integer = Data.NthField(".", 1).ToInteger
var iMin as integer = Data.NthField(".", 2).ToInteger
var iWee as integer = Data.NthField(".", 3).ToInteger
I’ll try you method in just a minute… I came up with this crude construct and it works.
' ---- Variable Defined ----
Var rsData As RowSet = modSel.mthGeneralSelect( "tblChgLog", "DESC" )
Var sIter As String = rsData.Column( "chgIter" ).StringValue
Var iMaj, iMin, iWee, i, x As Integer
Var sItem As String
' ---- Code In Action ----
i = sIter.Length
For z As Integer = 0 To i
If( sIter.Middle( z, 1 ) = "." ) Then
If( x = 0 ) Then
iMaj = sItem.ToInteger
x = 1
sItem = ""
Else
iMin = sItem.ToInteger
x = i - z - 1
iWee = sIter.Right( x ).ToInteger
txtIter.Text = iMaj.ToString + " " + iMin.ToString + " " + iWee.ToString
rsData.Close
Exit
End If
Else
sItem = sItem + sIter.Middle( z, 1 )
End If
Next
Sure thing! Just make sure you check to make sure your source data is in the right format before you parse it. You can use a regex for that or there are several examples floating around for an InStrCount function to see how many periods are in your source string.
Fortunately in this case just the 2. This is a personal program I am working on for myself to track work hours, estimated wages, time with my kids ( I share custody ), expenses and so forth. Essentially I have a program that tracks all the important aspect of my life that I need to keep tabs on in a format that works for me. If you have been through a divorce and child custody stuff… then what I am doing makes sense since I can produce accurate records of time and money flow in and out. And since there is nothing out there that does what I want in a single package… I am writing it! This is my 6th iteration of the program and it’s by far the best.
I have reduced the overall code size by close to 70% with a lot of the things I have learned both from here and from doing. It’s like a light bulb goes on and I see a cleaner way to do what I want. And you just helped me do that again!
Public Function Get3Parts(dotString As String) As String()
dotString = dotString + ".."
Var r() As String = dotString.Split(".")
Return Array(r(0), r(1), r(2))
End Function
Public Function Get3Ints(dotString As String) As Integer()
dotString = dotString + ".."
Var r() As String = dotString.Split(".")
Return Array(r(0).ToInteger, r(1).ToInteger, r(2).ToInteger)
End Function