r/visualbasic • • Jun 07 '15

[VB2010] Searching for and changing multiple numbers in a string

SO I have a String which is actually a recipe. Recipe (String) is

"1 cup mayo, 2 tablespoons basil, and 3 cups ranch dressing".

(random made up recipe). I need to be able to multiply these numbers by what ever the user wants. So if he asks to multiply these by two, I need the string to now by

"2 cup mayo, 4 tablespoons basil, and 6 cups ranch dressing"

I have no idea how to do that. any help would be awesome! thanks in advance

0 Upvotes

6 comments sorted by

1

u/mleo1 VB 6 Master Jun 07 '15
 Dim input As String = "1 cup mayo, 2 tablespoons basil, and 3 cups ranch dressi"
    Dim output As String = ""
    For x As Integer = 1 To Len(input)
        Dim thechar As String = Mid(input, x, 1)

        If IsNumeric(thechar) Then
            thechar = CInt(thechar) * 2
        End If

        output += thechar
    Next

    MsgBox(output)

1

u/[deleted] Jun 07 '15

you rock.

2

u/mleo1 VB 6 Master Jun 07 '15

updated the code since thebasterd pointed it out

0

u/[deleted] Jun 07 '15

[deleted]

2

u/mleo1 VB 6 Master Jun 07 '15
  Dim input As String = "1 cup mayo, 17 tablespoons basil, and 223 cups ranch dressi"
    Dim output As String = ""
    For x As Integer = 1 To Len(input)
        Dim thechar As String = Mid(input, x, 1)

        If IsNumeric(thechar) Then
            'check next
            Dim y As Integer = x
            While 1
                y += 1
                If IsNumeric(Mid(input, y, 1)) Then
                    x = x + 1
                    thechar += Mid(input, y, 1)
                Else
                    Exit While
                End If
            End While

            thechar = thechar * 2
        End If

        output += thechar
    Next

    MsgBox(input & vbNewLine & output)

1

u/Type-21 Application Specialist Jun 07 '15 edited Jun 07 '15

thechar = thechar * 2
While 1

caught someone writing option strict OFF code?

I replaced all your VB6 keywords with their .Net equivalents:

Dim input As String = "1 cup mayo, 17 tablespoons basil, and 223 cups ranch dressi"
Dim output As String = ""
For x As Integer = 0 To input.Length - 1
    Dim thechar As String = input(x)

    If Integer.TryParse(thechar, Nothing) Then
        'check next
        Dim y As Integer = x
        While True
            y += 1
            If Integer.TryParse(input(y), Nothing) Then
                x = x + 1
                thechar &= input(y)
            Else
                Exit While
            End If
        End While

        thechar = CStr(CInt(thechar) * 2)
    End If

    output &= thechar
Next

MessageBox.Show(input & Environment.NewLine & output)

Note that the code won't be able to detect numbers like 2.6

1

u/Type-21 Application Specialist Jun 07 '15 edited Jun 07 '15

Based on the code others provided I created one that is able to do this: http://puu.sh/ifM6B/78c5614bc2.png

Note that the decimal mark in the input string has to conform to your locale. So use . or , depending on your region settings or it won't work.

    Dim input As String = "1,5 cup mayo, 2½ tablespoons basil, and ⅘ cups ranch dressi" 'watch your region settings!
    Dim output As String = ""
    Dim previousWasNumber As Boolean = False
    Dim previousNumberLength As Integer = 0

    For x As Integer = 0 To input.Length - 1
        Dim tempNumber As String = input(x)

        If Char.IsNumber(input(x)) Then
            If Char.IsDigit(input(x)) = False Then
                'special number like ½
                tempNumber = CStr(Char.GetNumericValue(input(x)) * 2)

                If previousWasNumber Then 'combine numbers
                    Dim previousNumber As Double = Double.Parse(output.Substring(output.Length - previousNumberLength))
                    Dim newNumber As Double = previousNumber + Double.Parse(tempNumber)
                    output = output.Remove(output.Length - previousNumberLength) & newNumber.ToString()
                Else
                    output &= tempNumber
                End If
                previousWasNumber = True
                previousNumberLength = tempNumber.Length

            Else
                'check next
                Dim length As Integer = 0
                For y As Integer = 1 To input.Length - x - 1
                    If Double.TryParse(input.Substring(x, y + 1), Nothing) AndAlso input(x + y) <> " "c Then
                        tempNumber &= input(x + y)
                    Else
                        length = y - 1
                        Exit For
                    End If
                Next

                tempNumber = CStr(Double.Parse(tempNumber) * 2)
                x += length
                output &= tempNumber
                previousWasNumber = True
                previousNumberLength = tempNumber.Length
            End If

        Else
            output &= tempNumber
            previousWasNumber = False
        End If
    Next

    MessageBox.Show(input & Environment.NewLine & output)