首页 > 解决方案 > 在VB .Net中的子字符串之前修剪字符串

问题描述

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

前:

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

后:

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

我想修剪“(行”子字符串之前的字符串(也删除右侧剩余的所有字符)。在 VBA 中这很容易实现,但在 VB .Net 中它不是那么简单。请指导我一个可能的解决方案? 谢谢

标签: vb.net

解决方案


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

推荐阅读