Split string to NameValuecollection

  • Thread starter Thread starter Alex Schlecht
  • Start date Start date
A

Alex Schlecht

I have a string like this:

key1="value 1 with blanks" key2="another value"

How can I import this two pairs of Name/Value into a
NameValuecollection?

I would like to access to the values like:

nvlist.get("key1")


Thanks for help,

Alex
 
First, you have to break the whole string so you have the following:

0 - key1="somevalue"
1 - key2="somevalue"

then you can split again into a NameValueCollection

heres the whole thing:
string myLongString = @"key1="value 1 with blanks" key2="another
value"";
string[] myPairs = myLongString.Split(" ");

NameValueCollection nvc = new NameValueCollection();

foreach (string s in myPairs) {
string myKey = s.SubString(0, s.LastIndexOf('=') + 1);
string myValue = s.SubString(s.LastIndexOf('=') + 1, s.Length -
s.LastIndexOf('='));
nvc.Add(myKey, myValue);
}


I did not test that code, not sure if the substring parts are correct,
but you get the idea.

Someone might have an easier way, but thats how I would probably do it
 
First, you have to break the whole string so you have the following:

0 - key1="somevalue"
1 - key2="somevalue"

then you can split again into a NameValueCollection

heres the whole thing:
string myLongString = @"key1="value 1 with blanks" key2="another
value"";
string[] myPairs = myLongString.Split(" ");

NameValueCollection nvc = new NameValueCollection();

foreach (string s in myPairs) {
string myKey = s.SubString(0, s.LastIndexOf('=') + 1);
string myValue = s.SubString(s.LastIndexOf('=') + 1, s.Length -
s.LastIndexOf('='));
nvc.Add(myKey, myValue);
}


I did not test that code, not sure if the substring parts are correct,
but you get the idea.

Someone might have an easier way, but thats how I would probably do it
 
dkode said:
heres the whole thing:
string myLongString = @"key1="value 1 with blanks" key2="another
value"";
string[] myPairs = myLongString.Split(" ");

Well, that's just not going to work. Split will ignore the quotes, so
what you'll get in myPairs is:

key1="value
1
with
blanks"
key2="another
value"
 
Alex said:
I have a string like this:

key1="value 1 with blanks" key2="another value"

How can I import this two pairs of Name/Value into a
NameValuecollection?

I would like to access to the values like:

nvlist.get("key1")

string str = "key1= \"value 1 with blanks\" key2 =\"another value\"";

Regex regex = new
Regex("(?<Key>[^= ]+)\\s*=\\s*\"(?<Value>[^\"]+)\"\\s+");

MatchCollection matches = regex.Matches(str);
NameValueCollection nvc = new NameValueCollection();

foreach(Match m in matches)
{
string key = m.Groups["Key"].Value;
string value = m.Groups["Value"].Value;
nvc.Add(key, value);

Console.WriteLine("[{0}] = [{1}]", key, value);
}
 
James said:
string str = "key1= \"value 1 with blanks\" key2 =\"another value\"";

Regex regex = new
Regex("(?<Key>[^= ]+)\\s*=\\s*\"(?<Value>[^\"]+)\"\\s+");

MatchCollection matches = regex.Matches(str);
NameValueCollection nvc = new NameValueCollection();

foreach(Match m in matches)
{
string key = m.Groups["Key"].Value;
string value = m.Groups["Value"].Value;
nvc.Add(key, value);

Console.WriteLine("[{0}] = [{1}]", key, value);
}


Wow, works great! Thank you - now I have to learn how it works ;)
RegEx - a closed book for me. Still....
 
Back
Top