Splitting Name and Value Parameters

J

Jeff Williams

I have a string which is formated like this

Name1=Value1; Name2=Value2; Name3=Value3

I need to split this to an array of

Name1=Value1
Name2=Value2
Name3=Value3

What is the best way to split the Name and Values so I can place them
into an array.

Regards
Jeff
 
M

Marc Gravell

Random thought; this matches the syntaz for a connection string, so
just set this as the ConnectionString of a DbConnectionStringBuilder
instance, and you can parse the keys and item members. Sneaky but it
should work ;-p
Alternatively, you could use string.Split specifying new char[]
{'=',';'}

Marc
 
F

Freiddy

Random thought; this matches the syntaz for a connection string, so
just set this as the ConnectionString of a DbConnectionStringBuilder
instance, and you can parse the keys and item members. Sneaky but it
should work ;-p
Alternatively, you could use string.Split specifying new char[]
{'=',';'}

Marc

The second method should also involve string.Trim() because there is
the chance of spaces between the semicolons.

Freiddy
http://fei.yuanbw.googlepages.com/
http://freiddy.blogspot.com/\
 
V

VJ

Good idea. Not sneaky at all, just being smart about what data we are
handling and trying to do quick, easy and efficient solution.

VJ
 
T

Tom Dacon

Split on the semicolon, then loop through the output array splitting on the
equals sign.

Tom Dacon
Dacon Software Consulting
 
O

Otis Mukinfus

Good idea. Not sneaky at all, just being smart about what data we are
handling and trying to do quick, easy and efficient solution.

VJ

Marc Gravell said:
Random thought; this matches the syntaz for a connection string, so
just set this as the ConnectionString of a DbConnectionStringBuilder
instance, and you can parse the keys and item members. Sneaky but it
should work ;-p
Alternatively, you could use string.Split specifying new char[]
{'=',';'}

Marc

Not if you want a maintenance programmer or debugging person to be able to
understand what you did.

Just my $.02 worth...

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
 
M

Marc Gravell

Not if you want a maintenance programmer or debugging person to be able to
understand what you did.

There will be a lot less to debug in the first place if you re-use
tried-and-trusted code... and comments, e.g.

// expected input: key1=value1; key2=value2; key3=value3
// output: StringDictionary of parsed values from input

// use DbConnectionStringBuilder to parse input string
DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
StringDictionary dict = new StringDictionary();
builder.ConnectionString = value;
foreach(string key in builder.Keys) {
dict.Add(key, builder[key]);
}


Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top