I have string value like below:
var result = "{DimensionTransactionMonth=August, DimensionTransactionYear=2012}"
I need to transform like below:
var finalResult = "August,2012";
Does anyone know how to do it?
Here is a one liner so I can look clever, it doesn't depend on the Count
of Key Value Pairs:
var result = "{DimensionTransactionMonth=August, DimensionTransactionYear=2012}";
var stringValues = string.Join(",", result.Trim('{', '}').Split(',').Select(x => x.Split('=')[1]));
Console.Write(stringValues);
Output:
August,2012
Having said that, I would go with Sergey Berezovskiy's solution for the IDictionary
Lookup.
Explanation:
result.Trim('{', '}').Split(',').Select(x => x.Split('=')[1])
Broken Down:
1. .Trim('{', '}')
Removes the outer curly braces.
Result: "DimensionTransactionMonth=August, DimensionTransactionYear=2012"
2. .Split(',')
Turns the result of step one into an Array split on the ','
Result: ["DimensionTransactionMonth=August", "DimensionTransactionYear=2012"]
3. .Select(x => x.Split('=')[1])
Projects each item from step two into a IEnumerable<string>
as it does this it Splits
"DimensionTransactionMonth=August"
into ["DimensionTransactionMonth","August"]
and we take the Last value in that array "August"
No we can join this IEnumnerable<string>
back together using string.Join(',' ...)
like so:
string.Join(",", result.Trim('{', '}').Split(',').Select(x => x.Split('=')[1]));
Thus you are dealing with kev-value pairs, I would convert your input to Dictionary<string,string>
:
var result = "{DimensionTransactionMonth=August, DimensionTransactionYear=2012}";
var settings = result.Trim('{', '}')
.Split(',')
.Select(s => s.Trim().Split('='))
.ToDictionary(a => a[0], a => a[1]);
Now you can easily get final result:
var finalResult = String.Join(",", settings.Values); // "August,2012"
Or some specific value by its key:
var month = settings["DimensionTransactionMonth"]; // "August"
You need to do 3 steps (if you wish to do it manually like this).
Remove the brackets
var s = result.SubString(1, result.Length - 2);
Split the key-value pairs
var pairs = s.Split(new[]{','});
Extract the values by splitting on the =
sign
var resultset = pairs.Select(x => x.Split(new[]{'='})[1]).ToList();
var values = string.Join(",", result.Split(',').Select(o => o.Split(new[] { '=', '{', '}' })[1]));
Without regex (if there are only those two pairs):
string text = "{DimensionTransactionMonth=August, DimensionTransactionYear=2012}";
var token = text.Trim('{', '}').Split(',').Select(t => t.Trim());
string month = token.First().Split('=').Last().Trim();
string year = token.Last().Split('=').Last().Trim();
string finalResult = string.Format("{0},{1}", month, year);;
You could use string.Replace(); and string.Split();
var result = "{DimensionTransactionMonth=August, DimensionTransactionYear=2012}"
var splitValues = result.Replace("{","").Replace("}","").Split(",");
var month = splitValues[0].Split("=")[1];
var year = splitValues[1].Split("=")[1];
var finalResult = string.Format("{0},{1}", month, year);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.