1

I want to be able to paste the tab-delimited data (shown below) into a multi-line text box.

Data:

Fat 119.3g  62.1%
Saturated Fat   26.4g   
Cholesterol 442.5mg 
Sodium  3,824.8mg   
Carbohydrates   80.1g   18.5%
Fiber   12.1g   
Sugars  26.6g   
Protein 83.7g   19.4%

NOTE: Each row is: [nutrient name][tab][g/mg amount][tab][% (only on 3)]

When I click a button, I would like the following to happen:

fatInput.text = "119.3"
satfatInput.text = "26.4"
cholesteralInput.text = "442.5"
sodiumInput.text = "3824.8"
carbInput.text = "80.1"
fiberInput.text = "12.1"
sugarInput.text = "26.6"
proteinInput.text = "83.7"

So to summarize, it pulls those numbers out of the textbox I pasted it into and enters them into the separate textboxes. Commas, "g" and "mg" should not be included. The three percentages are not relevant and may be ignored. The nutrient names, order, and tabs do not change.

Is this even possible?!

THANKS FOR THE HELP!!!

Disclaimer: Not a programmer, just have basic knowledge and thought of a fun program to make to help me track what I eat. I've completed the program but decided to enhance it with the below so I don't have to manually type in these numbers. If this is too complicated I don't need the answer, just some direction and I can figure it out. I have no clue where to start!

2
  • UI Controls make horrible variables. Do this originate from some file? Commented Jan 24, 2016 at 23:27
  • It's actually from a website which is why I'd like to copy/paste it somewhere. Commented Jan 26, 2016 at 2:42

2 Answers 2

1

The TextBox has a Lines property giving you access to the text lines entered.

Then you can split the line further with the String.Split method

Dim parts() As String = line.Split(ControlChars.Tab)

VB has handy Val function that gets a number contained in a string. but it does not handle the comma, so remove it using the Replace function before getting the number

Dim d As Double = Val(parts(1).Replace(",", ""))

Then you can get the unit by looking at the two right most characters using the Right function and see whether it is "mg", if not check the right most character to see if it is a "g".

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this makes sense to me so far, I will give it a try tomorrow!
0

Thanks to Olivier for getting my brain thinking in the right order:

  1. use Lines property to put it an an array
  2. grab a single line from the array
  3. grab only numbers from the line (I used an if/then statement since Val kept returning "0")
  4. cut it off at "g" or "mg" (which was easy since I already had an if/then statement)
  5. repeat for each of the 8 lines

Probably nasty, but here's the code I used:

    Dim msgStr As String
    Dim newStr As String = ""
    Dim tempArray() As String
    tempArray = pasteInput.Lines
    msgStr = (tempArray(0))
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            fatInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next
    msgStr = (tempArray(1))
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            satfatInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next
    msgStr = (tempArray(2))
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            cholInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next
    msgStr = (tempArray(3))
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            sodiumInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next
    msgStr = (tempArray(4))
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            carbInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next
    msgStr = (tempArray(5))
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            fiberInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next
    msgStr = (tempArray(6))
    msgStr = Replace(msgStr, "Sugars", "")
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            sugarInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next
    msgStr = (tempArray(7))
    For Each value As Char In msgStr
        If value >= "0" And value <= "9" Or value = "." Then
            newStr &= value
        ElseIf value = "g" Or value = "mg" Then
            proteinInput.Text = newStr
            newStr = ""
            Exit For
        End If
    Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.