I want to be able to paste the tab-delimited data (shown below) into a multi-line text box.
: :
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%
: Each row is: [nutrient name][tab][g/mg amount][tab][% (only on 3)] :每行是: [营养素名称] [标签] [g / mg量] [标签] [%(仅适用于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!!!
: 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!
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".
Thanks to Olivier for getting my brain thinking in the right order:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.