开发者

searching a line and pulling a percentage out as an integer

So after my script runs a batch file it executes the shell command:

adb shell dumpsys cpuinfo > sample.txt

which then if you open sample.txt you see this:

0% 71/msm_battery: 0% user + 0% kernel <br>
0% 79/kondemand/0: 0% user + 0% kernel <br>
0% 115/rild: 0% user + 0% kernel <br>
0% 118/gpsd: 0% user + 0% kernel <br>
0% 375/com.android.systemui: 0% user + 0% kernel <br>
0% 415/com.nuance.nmc.sihome: 0% user + 0% kernel <br>
0% 498/com.google.process.gapps: 0% user + 0% kernel / faults: 6 minor <br>
0% 1876/com.wssyncmldm: 0% user + 0% kernel <br>

What im trying to do is if the user wants com.google.process.gapps it will return 0% from the text file. However this text file updates every second and com.google.process.gapps will not always be 0% and will not always be in the same place. I have figured out how to search for com.google.process.gapps and return the entire line as a string, what I havent figured out yet is how to search the entire file and just return the first 0% as just a 0 and as an integer instead of a string.

Dont worry about the repeating every second thing I already have that coded, all i need is help figuring out is how to write the search array and to return the first value as an int

Can anyone point me in the right direction?

........................................

I couldnt figure out the "add comment" thing so I am just reposting in here.

So if I go off your code I get this:

    Dim line As String = TextBox1.Text 'where textbox1 could equal com.google, etc.
    Dim Matches As MatchCollection = Regex.Matches(line, "[0-9]+%")
    For Each Match As Match In Matches
        Dim Percent As Integer = Integer.Parse(Match.Value.TrimEnd("%"c))
        TextBox9.Text = Percent
    Next

I know I 开发者_开发问答am missing one key part, and that is to load the entire text file.

maybe something like:

Dim searchfile As String = IO.File.ReadAllLines("C:\sample2.txt")

but then how would I Regex.matches(line, "[0-9]+%") in searchfile 'C:\sample2.txt

Thanks again for your help


Edit: To add to your ammended question...

The easiest way is to use RegEx. This code will extract every integer followed by a % symbol on the line as an Integer.

This function takes two parameters. The first is the search term, such as "com.google" which identifies the line to read. If the term is not found, the function throws an ArgumentException. The second parameter is which percentage value to read. Use 0 for the first, 1 for the second, or 2 for the third.

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1

    Public Function GetPercentage(term As String, percentage As Integer) As Integer

        ' Read all lines from the file.
        Dim lines As String() = File.ReadAllLines("C:\sample2.txt")

        ' Find the appropriate line in the file.
        Dim line As String
        Using reader As New StreamReader("C:\sample2.txt")
            Do
                line = reader.ReadLine()
                If line Is Nothing Then Throw New ArgumentException("The term was not found.")
                If line.Contains(term) Then Exit Do
            Loop
        End Using

        ' Extract the percentage value.
        Dim Matches As MatchCollection = Regex.Matches(line, "[0-9]+%")
        Dim Match As Match = Matches(percentage)
        Dim Text As String = Match.Value.TrimEnd("%"c)
        Return Integer.Parse(Text)
    End Function
End Class
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜