在VB .Net中在子字符串之前修剪字符串

时间:2019-02-04 12:08:08

标签: vb.net

我试图找到一种在子字符串之前修剪字符串的方法,即仅返回字符串的左侧。

之前:

[REMOVE] = 1 (Line0)
G77 H9002 (Line1)
[ZAXIS] = 25 (Line2)

之后:

[REMOVE] = 1
G77 H9002 
[ZAXIS] = 25 

我想修整“(行)子字符串之前的字符串(也删除所有保留在右边的字符)。 在VBA中,这很容易实现,但在VB .Net中,它并不是那么简单。 您能指导我解决一个问题吗? 谢谢

1 个答案:

答案 0 :(得分:0)

Dim strs = File.ReadLines("c:\\SomeFile.txt") 'Read the file

Using sw = File.CreateText("c:\Target")  'File to save to
    For Each str In strs
        Dim i = str.LastIndexOf("(Line") 'Find the index to cut
        Dim newStr = str.Substring(0, i) 'Cut the line at index
        sw.WriteLine(newStr)             'Write new string to new file.
    Next
End Using