从字符串vb.net剪切一个字符串

时间:2014-08-05 00:39:23

标签: vb.net string

Dim query As New ManagementObjectSearcher("select * from win32_processor") For Each cpu As ManagementObject In query.Get() txtCpu.Text = (cpu("Name"))

它为我提供了整个处理器名称:Intel(R)Core(TM)i7-2640M CPU @ 2.80GHz 我需要的只是2.80 所以我用mid来削减不想要的字符(假设每次我使用上面提到的代码,返回总是以处理器速度结束)    Mid(txtCpu.Text, Len(txtCpu.Text) - 6, Len(txtCpu.Text) - 3)
但答案总是在GHz = 2.80GHz

程序将在具有不同处理器的不同计算机上运行,​​因此我需要在没有单词ghz的情况下隔离速度,以便我能够看到计算机是否具有正确的要求

4 个答案:

答案 0 :(得分:0)

尝试Mid(txtCpu.Text, Len(txtCpu.Text) - 6, 3) - 最后一个参数是您想要的字符串的长度,而不是绝对位置。

您还可以从cpu["CurrentClockSpeed"]cpu["MaxClockSpeed"]

直接获取处理器速度(以MHz为单位)

答案 1 :(得分:0)

可能有更好的方法,但我提出了:

Dim proc As String = "Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz"
Debug.WriteLine(proc)
Dim index As Int32 = proc.IndexOf("@") 'Find the index of the @ symbol.
Debug.WriteLine(index)
proc = proc.Substring(index + 1).Trim 'Take a substring starting to the right of the @ symbol, then trim it.
Debug.WriteLine(proc)
proc = proc.Substring(0, proc.Length - 3) 'Take a substring that cuts off the GHz
Debug.WriteLine(proc)

这给了我:

Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz
31
2.80GHz
2.80

答案 2 :(得分:0)

试试这个:

Dim str As String = (Cpu("Name"))
txtCpu.Text = str.Substring(InStrRev(str, "@") + 1, (InStrRev(str, "GHz") - (InStrRev(str, "@") + 1)) - 1)

或者您可以通过以下代码直接获得时钟速度:

    Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_Processor")
    Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
    Dim proc As ManagementObject

    For Each proc In searcher.Get()
        txtCpu.text = proc("MaxClockSpeed") ' or proc("CurrentClockSpeed") . it will give you the frequency in MHz, you can convert it to GHz by dividing it by 1024

答案 3 :(得分:0)

简单来说,你可以在这里使用子字符串方法:

Dim str As String = "Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz"
Dim ghz As String = str.Substring(str.IndexOf("@") + 1, 5)

此处的输出将为2.80