I was seeing that in Python it is possible to format an unnumbered string the arguments .
Example:
#mensagem = "Meu nome é {0} e minha idade é {1}";
mensagem = "Meu nome é {} e minha idade é {}".format("Wallace", 27)
print(mensagem);
Is there any way in Csharp to set the formatting arguments positionally (ie omitting the number, just like in Python)?
Edit
I need something similar in C #. It would be very useful in a scenario where I need to concatenate strings that need to be formatted, but the numbering of the arguments would need to be dynamic.
Example:
var parameters = new Dictionary<string, string>() {
{"AND [a] = {0} ", TB_A.Text},
{"AND [b] LIKE '%{1}%' ", TB_B.Text},
{"AND [c] = '{2}' ", TB_C.Text},
{"AND [d] = '{3}' ", TB_D.Text}
};
foreach (var parameter in parameters)
{
// Se o valor for vazio, pula
// Isso quebra a sequência?
if (parameter.Value.Length > 0)
{
retorno += parameter.Key;
}
}
DataSource.FilterExpression = retorno;
DataSource.FilterParameters.Clear();
DataSource.FilterParameters.Add("A", TB_A.Text);
DataSource.FilterParameters.Add("B", TB_B.Text);
DataSource.FilterParameters.Add("C", TB_C.Text);
DataSource.FilterParameters.Add("D", TB_D.Text);
That way currently, when I have {1}
, but I do not have {0}
, the "formatting" for the {1}
value is being ignored. I did some tests and realized that if it obeys a straight positional order (eg 0,1,2,3), it works perfectly.