尝试从Visual Basic中的字符串中间删除一系列字符

时间:2017-01-14 19:39:05

标签: vb.net

我试图删除字符串的中间部分(' G0G90E1'和#39; M3S3000')。但是,从开始到结束的长度会有所不同,并且在' S'之后的数字会有所不同。也会有所不同(例如:' S500',' S10000'等)。我试图格式化这个字符串,它位于ShowContentsOfFile.Text中:

'G0G90E1X0Y0M3S3000
'X0Y0
'X1Y1

对此,但仅限于副本(显示在' E2'及以下),这是For,Next循环正在做的事情,因此,例如,它' ll看起来像这样,当我制作5个部分时:

'G0G90E1X0Y0M3S3000
'X0Y0
'X1Y1
'E2X0Y0
'X0Y0
'X1Y1
'E3X0Y0
'X0Y0
'X1Y1
'E4X0Y0
'X0Y0
'X1Y1
'E5X0Y0
'X0Y0
'X1Y1

不幸的是,它也摆脱了以下几行' E1':

'X0Y0
'X1Y1

这是我的代码:

    If NumberOfParts.Text = NumberOfParts.Text Then

        Dim CopySpecificText As String = ShowContentsOfFile.Text

        Dim Start As Integer = CopySpecificText.IndexOf("E1")
        Dim Finish As Integer = CopySpecificText.IndexOf("M")

        Dim DynamicString As String
        Dim x As Integer

        CopySpecificText = CopySpecificText.Replace("G0G90E1", "").Substring(Start, Finish - Start).Trim

        My.Computer.Clipboard.SetText(CopySpecificText)

        For x = 1 To NumberOfParts.Text - 1

            DynamicString = "E" & x + 1
            ShowContentsOfFile.Text += Environment.NewLine & DynamicString & Clipboard.GetText

        Next x

    End If

1 个答案:

答案 0 :(得分:0)

您可以将带有正则表达式的第一行拆分为字母+数字部分,例如" G0"," G90"," E1"," X0"," Y0"," M3& #34;," S3000"。

然后,您可以通过检查每个部件的第一个字符并将所选部件连接在一起来选择您想要重复的部件(例如" E1X0Y0")。

我认为" E"可能有一个不是1的数字,并且您希望从该数字开始并添加NumberOfParts中指定的部件数量。我还允许在第一行之后有一行或多行:

Option Infer On
Option Strict On

Imports System.Text.RegularExpressions

Public Class Form1

    Sub MakeNewText()
        Dim copySpecificText() As String = ShowContentsOfFile.Lines
        ' could use Integer.TryParse here to make sure a valid number has been entered:
        Dim nParts = CInt(NumberOfParts.Text)

        ' split the first line into letter-digits pieces and discard the resulting empty parts...
        Dim parts = Regex.Split(copySpecificText(0), "([A-Z][0-9]*)").Where(Function(p) Not String.IsNullOrWhiteSpace(p)).ToList()

        ' to show the parts which were found:
        'ShowContentsOfFile.AppendText(vbCrLf & "Found parts:")
        'For Each p In parts
        '   ShowContentsOfFile.AppendText(vbCrLf & p)
        'Next
        'ShowContentsOfFile.AppendText(vbCrLf & "---")

        ' extract the parts starting with certain letters into a string...
        Dim wantedCodes = "EXY".ToCharArray()
        Dim wantedParts = String.Join("", parts.Where(Function(p) wantedCodes.Contains(p.Chars(0))))

        ' get a string of the X and Y parts...
        Dim xyCodes = "XY".ToCharArray()
        Dim xyString = String.Join("", parts.Where(Function(p) xyCodes.Contains(p.Chars(0))))

        ' get the number from the "E" section:
        Dim eFirstIndex = CInt(parts.First(Function(p) p.StartsWith("E")).Substring(1))
        ' calculate the last index:
        Dim eLastIndex = eFirstIndex + nParts - 1

        For i = eFirstIndex To eLastIndex
            ShowContentsOfFile.AppendText(vbCrLf & "E" & i.ToString() & xyString)
            ShowContentsOfFile.AppendText(vbCrLf & String.Join(vbCrLf, copySpecificText.Skip(1)))

        Next

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MakeNewText()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' put test data into the text controls:
        NumberOfParts.Text = "5"
        ShowContentsOfFile.Text = "G0G90E1X0Y0M3S3000" & vbCrLf & "X0Y0" & vbCrLf & "X1Y1"

    End Sub

End Class

以上代码的结果:

G0G90E1X0Y0M3S3000
X0Y0
X1Y1
E1X0Y0
X0Y0
X1Y1
E2X0Y0
X0Y0
X1Y1
E3X0Y0
X0Y0
X1Y1
E4X0Y0
X0Y0
X1Y1
E5X0Y0
X0Y0
X1Y1

该代码应该比您尝试的代码更加可修改,虽然我猜它是G代码,但一旦您运行它就不需要修改程序。