Aaron
Aaron

Reputation: 3639

Split string after the = sign

I can't seem to work out how to get a value from my string using VB.net

If I have a string in my textbox that says:

WWW-Authenticate: Digest realm="MyServer",qop="auth",algorithm="MD5",maxbuf=1000,nonce="3b010c090c0a0000c0a80157c7007f03c5",opaque="4e6573732041636365737320436f6e74"

How can I get each of the values after the = in the string.

I have tried using

Dim s = "WWW-Authenticate: Digest realm='MyServer',qop='auth',algorithm='MD5',maxbuf=1000,nonce='3b010c090c0a0000c0a80157c7007f03c5',opaque='4e6573732041636365737320436f6e74'"
                        Dim pattern = "="
                        Dim matches = Regex.Matches(s, pattern)
                        Dim values = matches.OfType(Of Match).Select(Function(m) m.Value)

                        For Each v In values
                            MsgBox(v)
                        Next

But it only returns the = in the messagebox.

I want to be able to get just the part after the = sign.

Anyone able to help?

I have tried using the following but it still includes the realm= qop= etc.. in the string. (but includes it at the end of the next item.

 Dim s = "WWW-Authenticate: Digest realm='Ness Access Control',qop='auth',algorithm='MD5',maxbuf=1000,nonce='3b010c090c0a0000c0a80157c7007f03c5',opaque='4e6573732041636365737320436f6e74'"
                        Dim result_array As Array = Split(s, "=", 6)

                        For Each v In result_array
                            MsgBox(v)
                        Next

Upvotes: 0

Views: 1050

Answers (3)

Steve
Steve

Reputation: 216243

A case for a specific string extension. How to transform a specific formatted string in a Dictionary with keys and values

Public Module StringModuleExtensions
    <Extension()>
    Public Function ToStringDictionary(ByVal str as String, _
                                      ByVal OuterSeparator as Char, _
                                      ByVal  NameValueSeparator as Char) _
                                      As Dictionary(of String, String)
        Dim dicText = New Dictionary(Of String, String)()
        if Not String.IsNullOrEmpty(str) then
            Dim arrStrings() = str.TrimEnd(OuterSeparator).Split(OuterSeparator)
            For Each s in arrStrings
                Dim posSep = s.IndexOf(NameValueSeparator)
                Dim name = s.Substring(0, posSep)
                Dim value = s.Substring(posSep + 1)
                dicText.Add(name, value)
            Next
        End If
        return dicText
    End Function
End Module

Call with

Dim test = "WWW-Authenticate: Digest realm=""MyServer"",qop=""auth"",algorithm=""MD5"", maxbuf=1000,nonce=""3b010c090c0a0000c0a80157c7007f03c5"",opaque=""4e6573732041636365737320436f6e74"""
Dim dict = test.ToStringDictionary(","c, "="c)
For Each s in dict.Keys
    Console.WriteLine(dict(s))
Next

(probably you need to remove the WWW-Authenticate line before.

Upvotes: 0

Fabian Tamp
Fabian Tamp

Reputation: 4516

Regular Expressions!

Imports System.Text.RegularExpressions
Module Module1
    Sub Main()
        Dim s As String = "WWW-Authenticate: Digest realm='MyServer',qop='auth',algorithm='MD5',maxbuf=1000,nonce='3b010c090c0a0000c0a80157c7007f03c5',opaque='4e6573732041636365737320436f6e74'"

        'Regular Expression, matches word before equals, and word after equals
        Dim r As New Regex("(\w+)\='([^']+)'")

        'All the matches!
        Dim matches As MatchCollection = r.Matches(s)
        For Each m As Match In matches
            'm.Groups(1) = realm, qop, algorithm...
            'm.Groups(2) = MyServer, auth, MD5...
            Console.WriteLine(m.Groups(2))
        Next
        Console.ReadLine()
    End Sub
End Module

And if you want everything in a nice key-value dictionary:

    Dim dict As New Dictionary(Of String, String)
    For Each m As Match In matches
        'm.Groups(1) = realm, qop, algorithm...
        'm.Groups(2) = MyServer, auth, MD5...
        dict(m.Groups(1).ToString()) = dict(m.Groups(2).ToString())
    Next

Upvotes: 2

ficuscr
ficuscr

Reputation: 7054

You are looking for the split() function.

Dim logArray() As String
logArray = Split(s, "=")
For count = 0 To logArr.Length - 1
    MsgBox(logArray(count))
Next

Upvotes: 0

Related Questions