Question

I have a string like this:

+COPS:0,0,"XL",2 or +CSQ: "15",99

How can I get "XL" or "15" from the string.

On the second one, I've tried using replace and remove +CSQ: and ,99 but I can't do this on the first one.

Was it helpful?

Solution

For the first string use String.Split to split on commas then String.Trim to remove the quotes:

Dim line = "+COPS:0,0,""XL"",2"
' Split fields on comma
Dim fields = line.Split(",")
' Quote literal
Dim quote = """"c
' Use trim to remove quotes
Dim value = fields(2).Trim(quote)

OTHER TIPS

Same spirit as @Phillip Trelford but avoiding one step splitting on quotes :

Dim line = "+COPS:0,0,""XL"",2"
' Split fields on quotes
Dim fields = line.Split(""""c)
Dim value = fields(1)

One liner function :

Public Function getValue(ByVal from As String) As String
    Return from.Split(""""c)(1)
End Function 

You could use Regular expressions for that task. For example :

Dim stringtoscan = yourstringhere
Dim expression As New Text.RegularExpressions.Regex(Chr(34) + "(.*?)" + Chr(34))
Dim stringMatches = expression .Matches(stringtoscan)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top