Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 从字符串中获取数字_String_Vb.net_Text_Numbers - Fatal编程技术网

String 从字符串中获取数字

String 从字符串中获取数字,string,vb.net,text,numbers,String,Vb.net,Text,Numbers,我对使用Visual Basic编程非常陌生。我正在使用vb.net打开一个文本文件。我目前有一个程序,逐行读取文本文件,并将每一行添加到一个数组中 Private Function Read_GCode() As ArrayList Dim objReader As New System.IO.StreamReader(FILE_NAME) Dim TextLine As New ArrayList Do While objReader.Peek() <>

我对使用Visual Basic编程非常陌生。我正在使用vb.net打开一个文本文件。我目前有一个程序,逐行读取文本文件,并将每一行添加到一个数组中

 Private Function Read_GCode() As ArrayList
    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    Dim TextLine As New ArrayList
    Do While objReader.Peek() <> -1
        TextLine.Add(objReader.ReadLine())
    Loop
    Return TextLine
End Function
Private函数读取作为ArrayList的\u GCode()
Dim objReader作为新System.IO.StreamReader(文件名)
将文本行变暗为新的ArrayList
Do While objReader.Peek()-1
TextLine.Add(objReader.ReadLine())
环
返回文本行
端函数
我的文本文件将有如下内容

(*形状编号:0*)

G0 X2999.948 Y1771.567

M3 M8

G0 Z 0.000

G1 Z 0.000

F400

G1 X2999.948 Y 771.567

F0

G1 Z 0.000

G0 Z 0.000

M9 M5

G0 X 0.000 Y 0.000

M2(项目结束)

下一步是编写一个函数,从每行中获取数字。仅需要以“G”开头的行。如何从字符串中提取数字?

试试这个快速破解

Dim decList As New List(Of Decimal)
Dim tmp1 As Integer
Dim str As String = "G0 X2999.948 Y1771.567"
Dim started As Boolean = False
Dim result As String = ""
For i As Integer = 0 To str.Length() - 1
    If (started) Then
        If (Integer.TryParse(CStr(str(i)), tmp1) Or CStr(str(i)) = ".") Then
            result &= str(i)
        Else
            started = False
            decList.Add(Decimal.Parse(result))
            result = ""
        End If
    Else
        started = True
        If(Integer.TryParse(CStr(str(i)), tmp1) Then
            result &= str(i)
        End If
    End If
Next

上述代码应返回
decList

中的
2999.948
1771.567
以读取文本文件,您可以使用
file.ReadAllLines
返回行数组。为了解析这些行,我使用了各种
String
方法,并向
GData
对象添加了属性。最后,结果显示在
DataGridView
中的一个窗口中

Private GDataList As New List(Of GData)
Private Sub OPCode()
    Dim lines = File.ReadAllLines("G Code.txt")
    For Each line In lines
        If line.StartsWith("G") Then
            GDataList.Add(ParseG(line))
        End If
    Next
    DataGridView1.DataSource = GDataList
End Sub

Private Function ParseG(input As String) As GData
    Dim g As New GData
    Dim words = input.Split(" "c)
    Dim NextWordIndex As Integer
    g.GValue = CInt(words(0).Trim("G"c))
    If words(1).StartsWith("X") Then
        If words(1).Length > 1 Then
            g.XValue = CDbl(words(1).Trim("X"c))
            NextWordIndex = 2
        Else
            g.XValue = CDbl(words(2))
            NextWordIndex = 3
        End If
        If words(NextWordIndex).StartsWith("Y") Then
            If words(NextWordIndex).Length > 1 Then
                g.YValue = CDbl(words(NextWordIndex).Trim("Y"c))
            Else
                g.YValue = CDbl(words(NextWordIndex + 1))
            End If
        End If
    ElseIf words(1).StartsWith("Z") Then
        If words(1).Length > 1 Then
            g.ZValue = CDbl(words(1).Trim("Z"c))
        Else
            g.ZValue = CDbl(words(2))
        End If
    End If
    Return g
End Function

Public Class GData
    Public Property GValue As Integer
    Public Property XValue As Double
    Public Property YValue As Double
    Public Property ZValue As Double
End Class

您想提取文本文件中包含的所有数字,并将其显示在窗体或控制台应用程序中?文本文件中的所有行是否相似?多发布一点文本文件的内容。否。我遍历文本文件的每一行,因为每一行都有不同的含义,我需要为数字分配不同的变量。就像在这个例子中一样,我需要得到数字2999.948,并将其分配给变量x1和变量y1中的771.567。从文本文件中发布更多行以提高清晰度。添加更多文本文件内容。如果我希望两个数字都在分离的变量中而不是列表中,我该怎么办?只需执行
Dim I As Decimal=decList(0)
Dim j As Decimal=decList(1)
tmp1
用于临时存储
Integer.TryParse的输出。我尝试了该代码,并且
decList
中只有
0
2999.948
。它似乎在第一个和第二个数字之后停止。至少这是我在操作text.System.InvalidCastException后发现的:“从字符串”到类型“Double”的转换无效。”我得到了这个错误。我认为这是空间造成的。如何使用该函数处理空格?我对您提供的示例文本文件进行了测试。可以将字符串方法链接起来。您可以添加另一个
.Trim()
。空括号将从字符串的开头到结尾修剪一个空格。