2
I was seeing that in Python is possible format a string without numbering 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 define the formatting arguments in a positional way (i.e., omitting the number in the same way as in Python)?
Edit
I need something similar in C#. It would be very useful in a scenario where I need to concatenate strings that will need to be formatted, but argument numbering 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 these days, when I have {1}
, but I don’t have {0}
, the "formatting" for the value of {1}
is being ignored. I did some tests and realized that if he obeys a correct positional order (ex: 0,1,2,3), it works perfectly.
After marking as duplicate I realized that maybe it isn’t. What do you think, AP?
– Jéf Bueno
I don’t think it is. I need for example to assemble a string more or less with concatenation, but you can’t "guess" which condition will be accepted or not to concatenate. So the idea was to concatenate with "AND x = {}" and then use the
DS_Grid.FilterParameters.Add
with the right number of arguments (because playing like any number is giving error).– Wallace Maxters
I think your doubt should be focused on the specific problem. The current form is well with duplicate face that I pointed out.
– Jéf Bueno
@LINQ anyway, the duplication reference question does not answer regarding the "dynamic positional argument", in the sense that it ignores the number and takes the first occurrence from the
{}
. Anyway, Maniero’s "no" already answered.– Wallace Maxters
@Wallacemaxters Was that it? Need more?
– Maniero
@Wallacemaxters Was that it? Need more?
– Jéf Bueno