Brownd92 Brownd92 - 6 months ago 9
Vb.net Question

Need help Splitting a string in VB

I have a string which is being returned by a server and I need to get some information from it. This is the string:

(stock-info (msg-id 57) (reply-to 4) (ref "IC00000000234" std) (total (boxes 61)) (valid (boxes 61)) )

I need to get the total number of boxes and the valid boxes and put them into parameters. Ive tried using substrings and splitting the string into different parts, the problem is that this string could be different lengths so it dosent alway bring back the right result. Any help would be appreciated.

Thanks

Answer

There are many ways to solve your problem:

        Dim MyString As String = "(stock-info (msg-id 57) (reply-to 4) (ref ""IC00000000234"" std) (total (boxes 61)) (valid (boxes 61)) )"
    Dim SringExtracted As String = ""
    Dim MyKey As String = ""

    'Total Boxes
    MyKey = "(total (boxes"
    SringExtracted = MyString.Substring(MyString.LastIndexOf(MyKey) + MyKey.Length)
    SringExtracted = Microsoft.VisualBasic.Left(SringExtracted, SringExtracted.IndexOf("))"))
    Dim TotalBoxes As Integer = Val(SringExtracted)

    'Valid Boxes
    MyKey = "valid (boxes"
    SringExtracted = MyString.Substring(MyString.LastIndexOf(MyKey) + MyKey.Length)
    SringExtracted = Microsoft.VisualBasic.Left(SringExtracted, SringExtracted.IndexOf("))"))
    Dim ValidBoxes As Integer = Val(SringExtracted)