在vb.net

时间:2015-04-28 16:07:57

标签: vb.net string substring

我有以下字符串,并且需要在点之后提取切割为单个数字的X和Y值。

A234X78.027Y141.864D1234.2

这里可以改变一些变量:

  • 字符串可以包含任意长度并包含任意数量的值
  • 我知道X和Y总是存在,但它们不必在字符串中按特定顺序排列
  • X或Y的每个值都可以有任何长度..例如x可以是1.1或1234.1
  • X或Y确实没有必要。它也可以是一个圆数,例如X78Y141.34561(注意X没有点)如果没有点我没有值,但是如果有一个点那么我需要点之后的第一个数字。 (四舍五入)

作为上述字符串的结果,我需要两个包含值78.0和141.9的字符串变量

编辑:更新了最后一句,变量应该包含JUST值,没有X和Y.抱歉错误

根据要求更新代码

        Dim objReader As New System.IO.StreamReader(FILE_NAME)

        Do While objReader.Peek() <> -1
            Dim curline As String = objReader.ReadLine()  'curline = G1X39.594Y234.826F1800.0
            If curline.Contains("X") Then
                Dim t As String = ExtractPoint(curline, "X"c) 't = "39.594"
                Dim d As Double = Math.Round(Convert.ToDouble(t), 1) 'd= 39594.0
                destx = d * 10 'destx = 395940

            End If
         Loop
Function ExtractPoint(dataString As String, character As Char) As String
    Dim substring As String = String.Empty

    Dim xIndex As Integer = dataString.IndexOf(character) + 1
    substring += dataString(xIndex)
    xIndex = xIndex + 1
    While (xIndex < dataString.Length AndAlso Char.IsLetter(dataString(xIndex)) = False)
        substring += dataString(xIndex)
        xIndex = xIndex + 1
    End While

    Return substring
End Function

3 个答案:

答案 0 :(得分:1)

您是否查看了正则表达式?

    Dim x As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "X\d+([.]\d{1})?")
    Dim y As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "Y\d+([.]\d{1})?")

    MsgBox(x.ToString & " -- " & y.ToString)

如果我理解正确,我相信这会做你想要的。

编辑仅获取X和Y后的数字

根据原始代码,你可以做这样的事情。 这也将数字四舍五入到最接近的一位小数。

    Dim x As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "X(\d+([.]\d{2})?)")
    Dim y As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "Y(\d+([.]\d{2})?)")

    MsgBox(Math.Round(CDbl(x.Groups(1).Value), 1) & " -- " & Math.Round(CDbl(y.Groups(1).Value), 1))

更新了已添加代码的代码

    Dim s As String = "A234X78.027Y141.864D1234.2"
    Dim dX As Double = Extract(s, "X")
    Dim dY As Double = Extract(s, "Y")
    MsgBox(dX * 10 & "-" & dY * 10)

    Private Function Extract(ByRef a As String, ByRef l As String) As Double
        Dim x As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(a, l & "(\d+([.]\d{2})?)")
        Return Math.Round(CDbl(x.Groups(1).Value), 1)
    End Function

答案 1 :(得分:1)

您的示例数据表明字段以字母分隔,最后一个字母以字符串结尾。知道您可以手动解析所需的字母并舍入到小数点后1位。

这也会在没有小数点时考虑,但会在数字末尾显示.0。

修改

将公共代码移动到函数

更新

不包括该字母作为输出的一部分

Sub Main()
    Dim dataString As String = "G1X39.594Y234.826F1800.0"

    Dim xString As String = ExtractPoint(dataString, "X"c)
    Dim yString As String = ExtractPoint(dataString, "Y"c)

    Dim xDouble As Double = Math.Round(Convert.ToDouble(xString), 1)
    Dim yDouble As Double = Math.Round(Convert.ToDouble(yString), 1)

    Console.WriteLine(xDouble.ToString("F1"))
    Console.WriteLine(yDouble.ToString("F1"))

    Console.WriteLine((xDouble * 10).ToString("F1"))
    Console.WriteLine((yDouble * 10).ToString("F1"))

    Console.ReadLine()
End Sub

Function ExtractPoint(dataString As String, character As Char) As String
    Dim substring As String = String.Empty

    Dim xIndex As Integer = dataString.IndexOf(character) + 1
    substring += dataString(xIndex)
    xIndex = xIndex + 1
    While (xIndex < dataString.Length AndAlso Char.IsLetter(dataString(xIndex)) = False)
        substring += dataString(xIndex)
        xIndex = xIndex + 1
    End While

    Return substring
End Function

结果:

enter image description here

答案 2 :(得分:0)

这是一个简单的LINQ函数,应该为你做(没有正则表达式,没有长代码):

Private Function ExtractX(s As String, symbol As Char) As String
    Dim XPos = s.IndexOf(symbol)
    Dim s1 = s.Substring(XPos + 1).TakeWhile(Function(c) Char.IsDigit(c)).ToArray()
    If (XPos + 1 + s1.Length < s.Length) AndAlso s.Substring(XPos + 1 + s1.Length)(0) = "."c AndAlso Char.IsDigit(s.Substring(XPos + 1 + s1.Length)(1)) Then
      Return String.Join("", s1, s.Substring(XPos + 1 + s1.Length, 2))
    Else
      Return s1
    End If
End Function

这样称呼:

Dim s = "A234X78.027Y141.864D1234.2"
Dim x = ExtractX(s, "X"c)
Dim y = ExtractX(s, "Y"c)