仅显示文本文件中特定行的前 36 个字符

Only display the first 36 chacters of a specific line from a text file

我目前正在读取文本文件 speedtest.txt 以显示测试结果。该文件很长,我只需要一行中的部分信息即可显示在文本块中。代码看起来像这样

For Each line As String In File.ReadLines("c:\temp\logs\speedtest.txt")
        If line.Contains("MB/s") Or line.Contains("KB/s") Then
            TextBlock1.Text &= line & vbNewLine & vbCrLf
        End If
    Next line

输出如下所示:

2015-01-26 08:39:45 (1.29 MB/s) - 'test10.zip' 已保存 [11536384/11536384]

但我只需要这个:

2015-01-2608:39:45(1.29MB/s)

任何帮助或建议都会很棒。感谢大家提前。

编辑:

谢谢大家!我得到了我需要的东西。新代码如下所示

'Search speedtest.txt for Speed Test Results and update to textblock1
    For Each line As String In File.ReadLines("c:\temp\logs\speedtest.txt")
        If line.Contains("MB/s") Or line.Contains("KB/s") And line.Contains("saved") Then
            Dim speed As String = Microsoft.VisualBasic.Left(line, 34)
            TextBlock1.Text &= speed & vbNewLine & vbCrLf
            Exit For
        End If
    Next

VB 有一个名为 LEFT 的函数,它 returns 字符串的左侧部分。

Left(line, 36)

可以先求hypen的索引,再求子串

Dim myText = "2015-01-26 08:39:45 (1.29 MB/s) - 'test10.zip' saved [11536384/11536384]"
myText.Substring(0, myText.IndexOf(" - "))

输出将是:-

2015-01-26 08:39:45 (1.29 MB/s)

我认为您需要使用正则表达式,因为您知道自己想要什么,而且由于您有 36 个字符的限制,我认为您还需要在输出之前进行计数。

请参阅此处了解如何使用正则表达式。

http://www.codemag.com/Article/0305041