Splitting Name and Value Parameters

  • Thread starter Thread starter Jeff Williams
  • Start date Start date
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
 
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
 
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/\
 
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
 
Split on the semicolon, then loop through the output array splitting on the
equals sign.

Tom Dacon
Dacon Software Consulting
 
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
 
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
 
Back
Top