locked
extract key value pairs from a string RRS feed

  • Question

  • User-131059102 posted

    I have the following string "key=1Value=Foo key=2Value=Bar key=3Value=Baz"

    I need to get the keys and vaues for each item and write them out ot the page but I'm having issues trying to get the item at all. 

    Any help is appreciated.

    Monday, February 18, 2013 4:50 PM

Answers

  • User281315223 posted

    If you know and can guarantee that your values will be in the format that you provided (and will not contain any spaces) you can likely create a function that will handle seperating the strings into the values that you are looking for (using string.Split()).

    The following example will take your string and create a Dictionary mapping each Key to it's appropriate value : 

    string example = "key=1Value=Foo key=2Value=Bar key=3Value=Baz";
    string[] pairs = example.Split(' '); //yields ["key=1Value=Foo"],["key=2Value=Bar"],[key=3Value=Baz"]
    Dictionary<string, string> yourPairs = new Dictionary<string, string>();
    foreach (string pair in pairs)
    {
              string[] values = pair.Split(new string[] { "key=", "Value=" },StringSplitOptions.RemoveEmptyEntries);
              yourPairs.Add(values[0], values[1]);
    }
    
    
    

    Or if you want to get crazy, you can handle this in a single line using LINQ (albeit a big line) :

    string example = "key=1Value=Foo key=2Value=Bar key=3Value=Baz";
               
    Dictionary<string,string> test = example.Split(' ').ToDictionary(k => k.Split(new string[] { "key=", "Value=" },StringSplitOptions.RemoveEmptyEntries)[0], 
                                                                     v => v.Split(new string[] { "key=", "Value=" },StringSplitOptions.RemoveEmptyEntries)[1]);

    Or even smaller still!

    string values = "key=1Value=Foo key=2Value=Bar key=3Value=Baz";
    Dictionary<string, string> pairs = values.Split(' ').Select(s => s.Split(new string[] { "key=", "Value=" }, StringSplitOptions.RemoveEmptyEntries))
                                                        .ToDictionary(k => k[0], v => v[1]);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2013 7:12 PM

All replies

  • User465171450 posted

    Do any of the values have spaces in them?

    Monday, February 18, 2013 5:04 PM
  • User-131059102 posted

    This string is being stored inside a DB.... I can change the format of how its stored but it has to be some type of string and I need to get bot the Key and Value printed out if that mkase sense. 

    Monday, February 18, 2013 6:06 PM
  • User281315223 posted

    If you know and can guarantee that your values will be in the format that you provided (and will not contain any spaces) you can likely create a function that will handle seperating the strings into the values that you are looking for (using string.Split()).

    The following example will take your string and create a Dictionary mapping each Key to it's appropriate value : 

    string example = "key=1Value=Foo key=2Value=Bar key=3Value=Baz";
    string[] pairs = example.Split(' '); //yields ["key=1Value=Foo"],["key=2Value=Bar"],[key=3Value=Baz"]
    Dictionary<string, string> yourPairs = new Dictionary<string, string>();
    foreach (string pair in pairs)
    {
              string[] values = pair.Split(new string[] { "key=", "Value=" },StringSplitOptions.RemoveEmptyEntries);
              yourPairs.Add(values[0], values[1]);
    }
    
    
    

    Or if you want to get crazy, you can handle this in a single line using LINQ (albeit a big line) :

    string example = "key=1Value=Foo key=2Value=Bar key=3Value=Baz";
               
    Dictionary<string,string> test = example.Split(' ').ToDictionary(k => k.Split(new string[] { "key=", "Value=" },StringSplitOptions.RemoveEmptyEntries)[0], 
                                                                     v => v.Split(new string[] { "key=", "Value=" },StringSplitOptions.RemoveEmptyEntries)[1]);

    Or even smaller still!

    string values = "key=1Value=Foo key=2Value=Bar key=3Value=Baz";
    Dictionary<string, string> pairs = values.Split(' ').Select(s => s.Split(new string[] { "key=", "Value=" }, StringSplitOptions.RemoveEmptyEntries))
                                                        .ToDictionary(k => k[0], v => v[1]);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2013 7:12 PM