0

I have string variable that has value like this

string hello = "Endpoint: Efficacy, Intervene: Single Group, Mask: Open, Purpose: Treatment";

As I am extracting from XML so Endpoint , Intervene , Mask and Purpose remains same but its value can change. I want to store this Endpoint , intervene , mask and purpose separately in different variables any help will be much appreciated.

2
  • 1
    You shoudl deserialize your XML to an object wich matches your variables. Commented Aug 31, 2016 at 9:27
  • @MarcelTheis He should still parse that text Commented Aug 31, 2016 at 9:38

5 Answers 5

1
var result = hello.Split(',')
               .Select(part => part.Split(':'))
               .ToDictionary(split => split[0], split => split[1].Trim());
Sign up to request clarification or add additional context in comments.

1 Comment

could add trim on the key as well while being there. there is an extra space in front in the subsequent items. But this is the cleanest, readable solution.
1

Try this way using Dictionary

        string hello = "Endpoint: Efficacy, Intervene: Single Group, Mask: Open, Purpose: Treatment";
        Dictionary<string, string> result = new Dictionary<string, string>();
        var s1 = hello.Split(',');

        foreach (var s in s1)
        {
            var v = s.Split(':');
            result.Add(v[0].Trim(), v[1].Trim());
        }

After that you can get result using key value of the Dictionary

        foreach (var a in result)
        {
            Console.WriteLine(a.Key +" " + a.Value);
        }

Comments

0

Split your string first by , and then by : and save the result in dictionary:

public class Program
{
    public static void Main(string[] args)
    {
        string hello = "Endpoint: Efficacy, Intervene: Single Group, Mask: Open, Purpose: Treatment";
        string[] arr = hello.Split(',');
        Dictionary<string, string> result = new Dictionary<string, string>();
        foreach(string s in arr)
        {
            string[] keyValuePair = s.Split(':');
            result[keyValuePair[0].Replace(" ","")] = keyValuePair[1].Replace(" ","");

        }

        foreach(var v in result)
        {
            Console.WriteLine(v.Key + " : " + v.Value );
        }


    }
}

Comments

0

If the pattern is fixed, this can help you.

string hello = "Endpoint: Efficacy, Intervene: Single Group, Mask: Open, Purpose: Treatment";
string[] packs = hello.Split(',');
var data = packs.ToDictionary(k => k.Split(':').First().Trim(), v => v.Split(':').Last().Trim());

Comments

0

I came up with something more dynamic just in case someone have to deal with variable keywords to match.

This first extracts the keywords and builds a pattern to match. Matches are then captured by name and each capture group is named after a keyword so that we can access them by keyword:

var input = "Endpoint: Efficacy, Intervene: Single Group, Mask: Open, Purpose: Treatment";

//extract keywords: (word followed by ':' )
var keywords = Regex.Matches( input, @"(\w*):" ).Cast<Match>()
    .Select( m => m.Groups[ 1 ].Value );

//build the pattern to match (create capture groups named as keywords)
var pattern = String.Join( ", ", keywords.Select( 
    k => k + $@": (?<{k}>(\w*\s*)*)" ) );

var matches = Regex.Matches( input, pattern );

//access groups by keyword
foreach( var keyword in keywords )
    Console.WriteLine( matches[ 0 ].Groups[ keyword ].Value );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.