473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Converting "&" seperated string to Namevaluecollection

Hi

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

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

Regards
Amol Lokhande
Apr 3 '08 #1
3 2537
Plater
7,872 Expert 4TB
The Split() function would be probably be the optimal choice for that.


Another user with similar problem:
http://bytes.com/forum/thread790680.html
Apr 3 '08 #2
balabaster
797 Expert 512MB
Is that an asp.net QueryString? If it is, you would normally access the parts of the string using this mechanism:

Expand|Select|Wrap|Line Numbers
  1. Dim sName As String = Request.QueryString("name")
  2. Dim sCode As String = Request.QueryString("code")
  3. Dim sDept As String = Request.QueryString("dept")
Expand|Select|Wrap|Line Numbers
  1. string sName = Request.QueryString["name"];
  2. string sCode = Request.QueryString["code"];
  3. string sDept = Request.QueryString["dept"];
If it's not a QueryString, ignore me, Plater's idea was more appropriate...
Apr 3 '08 #3
balabaster
797 Expert 512MB
Or you could write a class that takes the string as an input and spits out a name value collection:
Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections.Specialized
  2. Public Class NameValueCollectionEx
  3.     Inherits NameValueCollection
  4.     Private _RecordDelimiter As String = "&"
  5.     Private _FieldDelimiter As String = "="
  6.     Public Property FieldDelimiter() As String
  7.         Get
  8.             Return _FieldDelimiter
  9.         End Get
  10.         Set(ByVal value As String)
  11.             _FieldDelimiter = value
  12.         End Set
  13.     End Property
  14.     Public Property RecordDelimiter() As String
  15.         Get
  16.             Return _RecordDelimiter
  17.         End Get
  18.         Set(ByVal value As String)
  19.             _RecordDelimiter = value
  20.         End Set
  21.     End Property
  22.     Public Property DataString() As String
  23.         Get
  24.             Dim sOut As String = Nothing
  25.             For Each sItem As String In MyBase.AllKeys
  26.                 sOut &= sItem & _FieldDelimiter & MyBase.Item(sItem) & _RecordDelimiter
  27.             Next
  28.             sOut = sOut.Substring(0, sOut.Length - 1)
  29.             Return sOut
  30.         End Get
  31.         Set(ByVal value As String)
  32.             Dim sData() As String = Split(value, _RecordDelimiter)
  33.             For Each sItem As String In sData
  34.                 MyBase.Add(Split(sItem, _FieldDelimiter)(0), Split(sItem, _FieldDelimiter)(1))
  35.             Next
  36.         End Set
  37.     End Property
  38.     Public Sub New()
  39.     End Sub
  40.     Public Sub New(ByVal NameValueString As String)
  41.         DataString = NameValueString
  42.     End Sub
  43.     Public Sub New(ByVal NameValueString As String, ByVal RecordDelimiter As String, ByVal FieldDelimiter As String)
  44.         _RecordDelimiter = RecordDelimiter
  45.         _FieldDelimiter = FieldDelimiter
  46.         DataString = NameValueString
  47.     End Sub
  48. End Class
  49. Module Module1
  50.     Sub Main()
  51.         Dim InputStr As String = "FirstName=Ben&MiddleName=&LastName=Alabaster&EmpId=259486"
  52.         Dim NVC As New NameValueCollectionEx(InputStr)
  53.         For Each Item As String In NVC.AllKeys
  54.             Console.WriteLine(Item & ": " & NVC(Item))
  55.         Next
  56.         Console.ReadLine()
  57.     End Sub
  58. End Module
As you can see from the Main() method, you can now just reference the name value collection:

Dim MyCol As New NameValueCollectionEx(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
File Type: txt Module1.vb.txt (2.1 KB, 369 views)
Apr 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David Furey | last post by:
Hi I have an XML documnet and a XSLT document as shown below THe XSLT document brings back a filtered docmument that has the VendorName that starts with a particular sub-string This works as...
2
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
5
by: Mateusz Loskot | last post by:
Hi, I'd like to ask how XML parsers should handle attributes which consists of &quot; entity as value. I know XML allows to use both: single and double quotes as attribute value terminator. That's...
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
0
by: Martin Maurer | last post by:
Hello, i have a problem with NameValueCollection.Add or better with converting to a QueryString: NameValueCollection myQueryStringCollection = new NameValueCollection();...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
5
by: Mark B | last post by:
Hi experts, I'm converting a homebrew AD management progam I wrote, from VB6 to VB 2008. I've got some code that sticks values in to Active Directory like this:- objOU.PutEx...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.