开发者

Read text file into array (Storing only numerical data)

I'm trying to read specific values from the text file (below):

Current Online Users: 0
Total User Log开发者_如何学Pythonins: 0
Server Uptime: 0 day, 0 hour, 0 minute
Downloaded Amount: 0.000 KB
Uploaded Amount: 0.000 MB
Downloaded Files: 0
Uploaded Files: 0
Download Bandwidth Utilization: 0.00 KB/s
Upload Bandwidth Utilization: 000.00 KB/s

I can read the file to an array:

   Dim path As String = "C:\Stats.txt"
   Dim StringArrayOfTextLines() As String = System.IO.File.ReadAllLines(path)

How do I store only the data I require in the array? I've tried split and substring but cannot work out a usable method - I need on the text after the colon for each line.

I was thinking, I only require the numerical data, can this be extracted from each line rather than just splitting into an array?

Thanks.


To capture everything after the colon you just need to split on it and take the second element of each result:

For Each s In StringArrayOfTextLines
    Console.WriteLine(s.Split(":")(1).Trim())
Next

If you want to do that as you read the data you'll need to use a StreamReader like Joel suggested.


ReadAllLines does just what it says it does. You have to iterate over the results. To read the data you want directly, you have to write the code to use a System.IO.StreamReader (and it's ReadLine() function) or even just a base System.IO.FileStream.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜