4

When using string.Format(string, object[]) it throws an exception if string contains more format specifiers ({0}, {1:dd-MM-yyyy} etc.) than object[].Length.

I'd like to also throw an exception if object[].Length contains more specifiers. There seems to be no built-in way to do it, so I'm trying to get the number of format specifiers in the input string. The tricky bit is that stuff like {{something}} or {0:dd-MM-yyyy} is allowed.

Does anyone know an easy or even built-in way to get the number of format specifiers in a string? I'm currently trying to build a regex, but maybe there is an easier way?

Michael Stum
  • 172,735
  • 112
  • 392
  • 530

1 Answers1

4

Looks like someone has already built a regex for this: Is there a better way to count string format placeholders in a string in C#?

Community
  • 1
  • 1
Ahmad Mageed
  • 91,579
  • 18
  • 159
  • 169