views:

57

answers:

1

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?

+2  A: 

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#?

Ahmad Mageed
Thanks, that looks good!
Michael Stum