Converting "&" seperated string to Namevaluecollection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amollokhande1
    New Member
    • Feb 2008
    • 33

    Converting "&" seperated string to Namevaluecollection

    Hi

    I have some string value like strTest="name=x yz&code=20&dept =10"

    How to convert this strTest to Namevaluecollec tion so to extract the individual value for key.

    Regards
    Amol Lokhande
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    The Split() function would be probably be the optimal choice for that.


    Another user with similar problem:

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Is that an asp.net QueryString? If it is, you would normally access the parts of the string using this mechanism:

      [Code=vb]Dim sName As String = Request.QuerySt ring("name")
      Dim sCode As String = Request.QuerySt ring("code")
      Dim sDept As String = Request.QuerySt ring("dept")[/Code]
      [Code=c]string sName = Request.QuerySt ring["name"];
      string sCode = Request.QuerySt ring["code"];
      string sDept = Request.QuerySt ring["dept"];[/Code]

      If it's not a QueryString, ignore me, Plater's idea was more appropriate...

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Or you could write a class that takes the string as an input and spits out a name value collection:
        [Code=vbnet]Imports System.Collecti ons.Specialized
        Public Class NameValueCollec tionEx
        Inherits NameValueCollec tion
        Private _RecordDelimite r As String = "&"
        Private _FieldDelimiter As String = "="
        Public Property FieldDelimiter( ) As String
        Get
        Return _FieldDelimiter
        End Get
        Set(ByVal value As String)
        _FieldDelimiter = value
        End Set
        End Property
        Public Property RecordDelimiter () As String
        Get
        Return _RecordDelimite r
        End Get
        Set(ByVal value As String)
        _RecordDelimite r = value
        End Set
        End Property
        Public Property DataString() As String
        Get
        Dim sOut As String = Nothing
        For Each sItem As String In MyBase.AllKeys
        sOut &= sItem & _FieldDelimiter & MyBase.Item(sIt em) & _RecordDelimite r
        Next
        sOut = sOut.Substring( 0, sOut.Length - 1)
        Return sOut
        End Get
        Set(ByVal value As String)
        Dim sData() As String = Split(value, _RecordDelimite r)
        For Each sItem As String In sData
        MyBase.Add(Spli t(sItem, _FieldDelimiter )(0), Split(sItem, _FieldDelimiter )(1))
        Next
        End Set
        End Property
        Public Sub New()
        End Sub
        Public Sub New(ByVal NameValueString As String)
        DataString = NameValueString
        End Sub
        Public Sub New(ByVal NameValueString As String, ByVal RecordDelimiter As String, ByVal FieldDelimiter As String)
        _RecordDelimite r = RecordDelimiter
        _FieldDelimiter = FieldDelimiter
        DataString = NameValueString
        End Sub
        End Class
        Module Module1
        Sub Main()
        Dim InputStr As String = "FirstName=Ben& MiddleName=&Las tName=Alabaster &EmpId=25948 6"
        Dim NVC As New NameValueCollec tionEx(InputStr )
        For Each Item As String In NVC.AllKeys
        Console.WriteLi ne(Item & ": " & NVC(Item))
        Next
        Console.ReadLin e()
        End Sub
        End Module[/Code]

        As you can see from the Main() method, you can now just reference the name value collection:

        Dim MyCol As New NameValueCollec tionEx(InputStr )

        You would reference the items as:

        MyCol("name")
        MyCol("code")
        MyCol("dept")
        etc...

        That class allows you to specify delimiters other than & and =. The field delimiter splits the record into key and value and the record delimiter splits the records. The defaults are & and = though.
        Attached Files
        Last edited by Plater; Apr 3 '08, 04:03 PM. Reason: colorize

        Comment

        Working...