Convert Sentence into formula VB.NET C#

Okay, this is not homework. But, I am looking for a method to input a string in and then calculate the resultant value.
I have a string such as "Eight Furlongs On The Dirt Track"
Now, I can pull out the "On The Dirt Track" part using a simple string.remove().

There are two formats to this string, and they are as follows:
1:     [number] [distance] On The [surface] <-Surface is irrelevent but dynamic as well
             example - "Eight Furlongs On The Dirt Track"
2:     [number] And [number] [distance] On The [surface]
             example - "One And One Sixteenth Miles On The Turf"

Any suggestions as to how to do this, VB.NET or C# will work.
Thomas_HawkinsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Thomas_HawkinsConnect With a Mentor Author Commented:
To anyone in this situation, here is the code I finally used. The experts where right in that there was no built-in way to do it. I was hoping to avoid the tedious code - but after several revisions I came up with this and it works marvelously, it tackles any situation I might have using furlongs, miles,  yards, and even feet to a degree of precision of sixteenths.

I only attach this code to help anyone looking for a roll your own situation - I know how frustrating it can be.
Public conversionWhole(,) As String = {{"sixteenth", "0.0625"}, {"eighth", "0.0125"}, {"quarter", "0.25"}, {"half", "0.5"},
                                   {"tenth", "0.1"}, {"one", "1"}, {"two", "2"}, {"three", "3"}, {"four", "4"}, {"five", "5"}, {"six", "6"}, {"seven", "7"}, {"eight", "8"}, {"nine", "9"}, {"ten", "10"},
                                   {"eleven", "11"}, {"twelve", "12"}, {"thirteen", "13"}, {"fourteen", "14"}, {"fifteen", "15"}, {"sixteen", "16"}, {"seventeen", "17"}, {"eighteen", "18"},
                                   {"nineteen", "19"}, {"twenty", "20"}, {"twenty-five", "25"}, {"thirty", "30"}, {"thirty-five", "35"}, {"forty", "40"}, {"forty-five", "45"}, {"fifty", "50"},
                                   {"fifty-five", "55"}, {"sixty", "60"}, {"sixty-five", "65"}, {"seventy", "70"}, {"seventy-five", "75"}, {"eighty", "80"}, {"eighty-five", "85"},
                                   {"ninety", "90"}, {"ninety-five", "95"}, {"hundred", "100"}}

Public Sub getRaceLengthType(ByVal str As String)
        str = str.ToLower
        'dMod holds the different lengths one might find in race lengths
        Dim dMod As Double = 0.0  ' This helps differentiate if these mods are used

        'num holds the different lengths one might find in race lengths
        Dim num As Double = 0.0

        Dim distance As Double = 0.0
        Dim r As Integer = 0

        'Place type of race inside raceType and trim it (Dirt, Turf, All Weather Track, etc)
        Dim raceType As String = str.Substring(str.IndexOf(" on ")).Trim
        raceType = raceType.Remove(0, 7).Trim

        'Here we would do something with the value in raceType - in my code it is sent to a public array. The variable raceType is reused later in the code.

        str = str.Substring(0, str.IndexOf(" on ")).Trim
        
        Dim pos As Integer

        Do Until str.Length = 0
            If str.Contains(" ") = False Then
                raceType = str.Substring(0)
            Else
                pos = str.IndexOf(" ")
                raceType = str.Substring(0, pos).Trim
            End If

            If raceType = "mile" Or raceType = "miles" Then
                dMod = 5280
                distance = distance + (num * dMod)
                num = 0
                If raceType = "miles" Then
                    pos = str.IndexOf("miles")
                    str = str.Remove(pos, 5).Trim
                ElseIf raceType = "mile" Then
                    pos = str.IndexOf("mile")
                    str = str.Remove(pos, 4).Trim
                End If
            ElseIf raceType = "furlong" Or raceType = "furlongs" Then
                dMod = 660
                distance = distance + (num * dMod)
                num = 0
                If raceType = "furlongs" Then
                    pos = str.IndexOf("furlongs")
                    str = str.Remove(pos, 8).Trim
                ElseIf raceType = "furlong" Then
                    pos = str.IndexOf("furlong")
                    str = str.Remove(pos, 7).Trim
                End If
            ElseIf raceType = "yard" Or raceType = "yards" Then
                dMod = 3
                distance = distance + (num * dMod)
                num = 0
                If raceType = "yards" Then
                    pos = str.IndexOf("yards")
                    str = str.Remove(pos, 5).Trim
                ElseIf raceType = "yard" Then
                    pos = str.IndexOf("yard")
                    str = str.Remove(pos, 4).Trim
                End If
            ElseIf raceType = "feet" Or raceType = "foot" Then
                dMod = 1
                distance = distance + (num * dMod)
                num = 0
                If raceType = "foot" Then
                    pos = str.IndexOf("foot")
                    str = str.Remove(pos, 4).Trim
                ElseIf raceType = "feet" Then
                    pos = str.IndexOf("feet")
                    str = str.Remove(pos, 4).Trim
                End If
            ElseIf raceType = "and" Then
                str = str.Replace(raceType, "").Trim
            ElseIf raceType = "hundred" Then
                pos = str.IndexOf("hundred")
                num = num * 100
                str = str.Remove(0, 7).Trim
            Else
                r = 0
                Do Until r = 41
                    If raceType = conversionWhole(r, 0) Or raceType = conversionWhole(r, 0) & "s" Then
                        pos = str.IndexOf(raceType)
                        If CType(conversionWhole(r, 1), Double) < 1 Then
                            num = num * CType(conversionWhole(r, 1), Double)
                        Else
                            num = num + CType(conversionWhole(r, 1), Double)
                        End If
                        str = str.Remove(0, raceType.Length).Trim
                    End If
                    r += 1
                Loop
            End If

        Loop

        testlbl.Text = distance.ToString

Open in new window

0
 
Thomas_HawkinsAuthor Commented:
Dhaest - that converts numbers to text, I need to convert text to numbers.

For example, I have "Eight furlongs on the dirt". I remove "on the dirt" and need to convert string "Eight furlongs" into 8*660 or 5280. Of course sometimes it can be different, some times it could be something like "One and One Quarter Mile on the dirt" in which case I would need something akin to (1*5280) + (.25*5280).

In the above "660" replaces furlong and "5280" replaces mile. Not that you guys wouldn't know that, just being overly explanatory.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
DhaestCommented:
WordStringToNumber.txtHree you find already the part to convert a word-number to a number

long myNumber = CreateNumber("Nine Thousand Two Hundred Seventy Three");
0
 
CmdoProg2Connect With a Mentor Commented:
You want to split the original string into whole number, fractional part, and distance after removing the surface part.  The uses the method of Dhaest's post.  You'll need to handle case sensitivity and pural forms.
StringSplitCSharp.txt
StringSplitVB.txt
0
 
Thomas_HawkinsAuthor Commented:
I only included my solution as best for people looking for this very thing. I wanted to provide them with a straight code solution. The two experts given points where right on the money!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.