Is it possible to omit the numbers of the "String.Format" formatting arguments in C#?

Asked

Viewed 136 times

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?

  • 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).

  • 1

    I think your doubt should be focused on the specific problem. The current form is well with duplicate face that I pointed out.

  • @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.

  • 1

    @Wallacemaxters Was that it? Need more?

  • 1

    @Wallacemaxters Was that it? Need more?

Show 1 more comment

1 answer

2


No, but you can interpolation:

mensagem = $"Meu nome é {("Wallace")} e minha idade é {27}";

Of course, if it’s not variable or some expression that involves variable, it doesn’t make sense.

Responding to the issue:

public class Program { public static void Main() => System.Console.WriteLine("{0}, {2}, {3}", 1, null, "teste", 23.5); }

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I still find this solution horrible, should not ride darlings so. Years of gambiarra in PHP gives it :P

Learning to do MCVE is good :P

  • The values were just an example, but the idea was the same variable. And in my case, the problem is that the class that controls the formatting is the ParameterCollection of SqlDataSource. But the "no" already helps to know that I will have to go another way, kkkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.