String has how many parameters

前端 未结 3 2405
既然无缘
既然无缘 2021-02-20 15:39

Before using String.Format to format a string in C#, I would like to know how many parameters does that string accept?

For eg. if the string was \"{0} is not the same

3条回答
  •  一个人的身影
    2021-02-20 16:38

    You'll have to parse through the string and find the highest integer value between the {}'s...then add one.

    ...or count the number of sets of {}'s.

    Either way, it's ugly. I'd be interested to know why you need to be able to figure out this number programatically.

    EDIT

    As 280Z28 mentioned, you'll have to account for the various idiosyncrasies of what can be included between the {}'s (multiple {}'s, formatting strings, etc.).

提交回复
热议问题