Reputation: 75
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
Upvotes: 0
Views: 52
Reputation: 164
You could use regular expressions for this situation. https://msdn.microsoft.com/en-us/library/hs600312(v=vs.110).aspx
Imports System.Text.RegularExpressions
Dim input As String = "(stock-info (msg-id 57) (reply-to 4) (ref ""IC00000000234"" std) (total (boxes 60)) (valid (boxes 61)) )"
For Each match As Match In Regex.Matches(input, "\(total \(boxes (\d+)\).+\(valid \(boxes (\d+)\)")
Console.WriteLine("Total={0}, Valid={1}", match.Groups.Item(1).Value, match.Groups.Item(2).Value)
Next
Upvotes: 1
Reputation: 25013
This is the sort of problem where a regular expression can be useful:
Option Infer On
Option Strict On
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim s = "(stock-info (msg-id 57) (reply-to 4) (ref ""IC00000000234"" std) (total (boxes 61)) (valid (boxes 48)) )"
Dim reTotal As New Regex("total \(boxes ([0-9]+)")
Dim reValids As New Regex("valid \(boxes ([0-9]+)")
' initialise with a value than cannot appear in the string
' so it can be checked for later if needed
Dim totalBoxes As Integer = -1
Dim validBoxes As Integer = -1
If reTotal.IsMatch(s) Then
totalBoxes = CInt(reTotal.Match(s).Groups(1).Value)
End If
If reValids.IsMatch(s) Then
validBoxes = CInt(reValids.Match(s).Groups(1).Value)
End If
Console.WriteLine($"Total: {totalBoxes} Valid: {validBoxes}")
Console.ReadLine()
End Sub
End Module
Outputs:
Total: 61 Valid: 48
Upvotes: 2
Reputation: 2750
This should work. Splits the string by "boxes ", ignores the first part of the string, and then strips out all non-numeric characters for the remaining two strings (in char array form) before converting them back to a string. If you want them as integer just parse them afterwards.
Dim myString = "(stock-info (msg-id 57) (reply-to 4) (ref 'IC00000000234' std) (total (boxes 61)) (valid (boxes 61)) )"
Dim splitArray = myString.Split("boxes ").Skip(1)
Dim totalBoxesAsString = New String(splitArray.First.Where(Function(c) Char.IsDigit(c)).ToArray())
Dim validBoxesAsString = New String(splitArray.Last.Where(Function(c) Char.IsDigit(c)).ToArray())
Upvotes: 1
Reputation: 318
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)
Upvotes: 1