因此,在我的脚本运行批处理文件后,它会执行shell命令:
adb shell dumpsys cpuinfo > sample.txt
然后,如果你打开sample.txt,你会看到:
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>
我想要做的是,如果用户想要com.google.process.gapps,它将从文本文件返回0%。但是,此文本文件每秒更新一次,com.google.process.gapps不会始终为0%,并且不会始终位于同一位置。我已经找到了如何搜索com.google.process.gapps并将整行作为字符串返回,我还没想到的是如何搜索整个文件,只返回0%作为0和as
。整数而不是字符串。不要担心重复我已经编码的每一件事,我只需要帮助弄清楚如何编写搜索数组并将第一个值作为int
有人能指出我正确的方向吗?
........................................
我无法弄清楚“添加评论”的事情所以我只是在这里重新发布。
因此,如果我关闭您的代码,我会得到这个:
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
我知道我缺少一个关键部分,那就是加载整个文本文件。
可能是这样的:
Dim searchfile As String = IO.File.ReadAllLines(“C:\ sample2.txt”)
但是我怎样才能在搜索文件'C:\ sample2.txt
中使用Regex.matches(line,“[0-9] +%”)再次感谢您的帮助
答案 0 :(得分:0)
修改:添加到您修改后的问题中......
最简单的方法是使用RegEx。此代码将提取每个整数,后跟行上的%符号作为整数。
此功能有两个参数。第一个是搜索词,例如“com.google”,用于标识要阅读的行。如果未找到该术语,则该函数将抛出ArgumentException。第二个参数是要读取的百分比值。第一个使用0,第二个使用1,第三个使用2。
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