Convert Sentence into formula VB.NET C#

Posted on 2012-04-10
Last Modified: 2012-04-16
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.
Question by:Thomas_Hawkins
  • 3
  • 2
LVL 53

Assisted Solution

Dhaest earned 250 total points
ID: 37831112

Author Comment

ID: 37831307
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.
LVL 53

Expert Comment

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

long myNumber = CreateNumber("Nine Thousand Two Hundred Seventy Three");
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 12

Assisted Solution

CmdoProg2 earned 250 total points
ID: 37832668
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.

Accepted Solution

Thomas_Hawkins earned 0 total points
ID: 37835848
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)
                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
                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)
                            num = num + CType(conversionWhole(r, 1), Double)
                        End If
                        str = str.Remove(0, raceType.Length).Trim
                    End If
                    r += 1
            End If


        testlbl.Text = distance.ToString

Open in new window


Author Closing Comment

ID: 37850402
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!

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question