Reputation: 171
In VB.NET I need to find a value inside of a substring within a larger string.
For example, MyString is a string
MyString = "
============================
Channel --> 36
Antenna --> 127
MCS Level --> 14
Bandwidth --> 20
Packet Size --> 4000
11N signal? --> 1
BF Config --> 0
============================"
If I need to find a particular value, say MCS level from that string, do I search for the location of "MCS Level --> " and find the value of the next 2 chars?
Or is there a way to search for "MCS Level --> %s" and find the value of %s within MyString?
Thanks! I appreciate the help. I want to do this properly.
Upvotes: 0
Views: 793
Reputation: 11233
You can try regex as well. Check with this pattern:
MCS Level\D+(\d+)
It will give you the desired result 14
.
EDIT
Sample Code: (untested, to give an idea)
Dim mcol as MatchCollection = System.Text.RegularExpression.Regex.Matches(MyString,"MCS Level\D+(\d+)")
for each m as match in mcol
Debug.Print("MCS Level is : " & m.ToString())
Next
Upvotes: 0
Reputation: 14088
For this particular example, I would first split the string by new lines, then split each line again by whitespace (ignoring empty occurrences). That leaves you with a jagged array containing every piece of non-whitespace text per line.
Function GetMcsLevel(myString As String) As Integer
For Each line As String In MyString.Split(Environment.NewLine)
Dim pieces() As String = line.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
If pieces.First() = "MCS Level" Then
Return Integer.Parse(pieces.Last())
End If
Next
End Function
Upvotes: 1