Is it possible to create an extension method to format a string?

后端 未结 9 1142
青春惊慌失措
青春惊慌失措 2021-01-12 00:56

the question is really simple. How do we format strings in C#? this way:

 string.Format(\"string goes here with placeholders like {0} {1}\", firstName, lastN         


        
9条回答
  •  盖世英雄少女心
    2021-01-12 01:03

    Yes.       

    public static string Format(this string str, string firstItem, string secondItem)
    {
        return string.Format(str, firstItem, secondItem);
    }
    

    BTW, this method has to belong to a public and static class.

提交回复
热议问题