473,698 Members | 2,503 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 2567
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, 374 views)
Apr 3 '08 #4

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

Similar topics

1
8061
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
20521
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
3457
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
14791
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
1091
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
3435
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
5929
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
2801
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
2368
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
8609
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
9169
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
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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

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.