将文本框中的数据解析为多个文本框 - VB.NET

时间:2016-01-24 23:11:56

标签: vb.net

我希望能够将制表符分隔的数据(如下所示)粘贴到多行文本框中。

数据

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%

注意:每行是: [营养素名称] [标签] [g / mg量] [标签] [%(仅限3)]

当我点击按钮时,我希望发生以下情况:

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"

总而言之,它将这些数字从我粘贴的文本框中拉出来并将它们输入到单独的文本框中。逗号," g"和" mg"不应该包括在内。这三个百分比无关紧要,可能会被忽略。营养素名称,顺序和标签不会改变。

这甚至可能吗?!

感谢帮助!

免责声明:不是程序员,只是掌握基本知识并想到一个有趣的程序来帮助我跟踪我吃的东西。我已完成该计划但决定使用以下内容进行增强,因此我不必手动输入这些数字。如果这太复杂了,我不需要答案,只是某个方向,我可以弄明白。我不知道从哪里开始!

2 个答案:

答案 0 :(得分:1)

TextBox具有Lines属性,可让您访问输入的文本行。

然后,您可以使用String.Split方法

进一步分割线条
Dim parts() As String = line.Split(ControlChars.Tab)

VB有一个方便的Val函数,它获取一个包含在字符串中的数字。但它不处理逗号,因此在获取数字

之前使用Replace函数将其删除
Dim d As Double = Val(parts(1).Replace(",", ""))

然后你可以通过使用Right函数查看最右边的两个字符来获取单位,看看它是否是“mg”,如果没有检查最右边的字符,看它是否是“g”

答案 1 :(得分:0)

感谢Olivier以正确的顺序让我的大脑思考:

  1. 使用Lines属性将其设为数组
  2. 从阵列中抓取一行
  3. 只抓取该行中的数字(我使用了if / then语句,因为Val保持返回" 0")
  4. 在" g"或" mg" (这很简单,因为我已经有了if / then声明)
  5. 重复8行中的每一行
  6. 可能很讨厌,但这里是我使用的代码:

        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