473,738 Members | 5,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting "&" seperated string to Namevaluecollec tion

33 New Member
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
Apr 3 '08 #1
3 2571
Plater
7,872 Recognized Expert Expert
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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 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
File Type: txt Module1.vb.txt (2.1 KB, 375 views)
Apr 3 '08 #4

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

Similar topics

1
8064
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 expected with alphabet and number characters and the ' (single quote ' entity) character but does not work if a double quote character " is part of the string to filter on This returns all Vendor Names that begin with A (either case)
2
20529
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
3459
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 clear. But how should parser react for such situation: I have CORDSYS element with string attribute which consists of value with many &quot; entities:
4
14801
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 expression: .... snip ...
0
1097
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(); myQueryStringCollection.Add("Test1", null); myQueryStringCollection.Add("Test2","abc"); WebClient myWebClient = new WebClient(); myWebClient.QueryString = myQueryStringCollection;
5
3443
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; &lt;me@mydomain.com&gt;</Address> </Addresses>
14
5934
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
13
2803
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 produced like: <title>my very long title blab la blab la Blabla bla </title> Importer also has got problems with it
5
2384
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 ADS_PROPERTY_APPEND, "wbempath, Array(Product & "|" and Key) Basically, a convenient place for me to store and manage CD installation keys.
0
8787
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.