How to get all string format parameters

Is there a way to get all the string format options?

I have this line: "{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?

+3
source share
5 answers

I have not heard of such built-in functions, but you could try this (I assume that your line contains standard format parameters starting with a 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 the values {0} {0} {1} {2:####}in the list. For tehMick, the result will be empty.

+2
source

, , .

, \{.*?\}, , .

+3

, . - .

+1

. , StringBuilder.AppendFormat(IFormatProvider, string, object []).

+1
0

Source: https://habr.com/ru/post/1742988/


All Articles