我目前正在阅读文本文件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-26 08:39:45(1.29 MB / 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
答案 0 :(得分:1)
你可以先找到连字符的索引,然后得到子串
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)
答案 1 :(得分:0)
VB有一个名为LEFT的函数,它返回一个字符串的左边部分。
Left(line, 36)
答案 2 :(得分:0)
我认为你需要使用正则表达式,因为你知道你想要什么,而且因为你有36个字符的限制我需要在输出它之前做计数。
请参阅有关如何使用正则表达式的信息。