Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all string format parameters

Is there a way to get all format parameters of a string?

I have this string: "{0} test {0} test2 {1} test3 {2:####}" The result should be a list: {0} {0} {1} {2:####}

Is there any built in functionality in .net that supports this?

like image 894
crauscher Avatar asked Jan 22 '23 04:01

crauscher


1 Answers

I didn't hear about such a build-in functionality but you could try this (I'm assuming your string contains standard format parameters which start with number digit):

List<string> result = new List<string>();
string input = "{0} test {0} test2 {1} test3 {2:####}";
MatchCollection matches = Regex.Matches(input, @"\{\d+[^\{\}]*\}");
foreach (Match match in matches)
{
    result.Add(match.Value);
}

it returns {0} {0} {1} {2:####} values in the list. For tehMick's string the result will be an empty set.

like image 103
Oleks Avatar answered Jan 29 '23 16:01

Oleks