I recently began to learn Ruby and I saw following way to format a string:
"%d" % 123 #=> "123"
"%08b %02x" % [15, 15] #=> "00001111 0f"
Is there a way to realize this in C#? I tried a lot, but all attempts failed. Here is a example of one of my attempts:
public static string operator % (string format, IFormatable[] items) {
for (int n = 0; n < items.Length; n++) format.Replace("%" + n, items[n].ToString());
return format;
}
"%0 %1" % new string[] { "Foo", "Bar" } // Expected result => "Foo Bar"
I know the Methods like ToString() and PadLeft(), but I wanted to find out, if the so-called Method Extensions also work for operators.