C#扩展String类,自定义ToFormat方法格式化字符串,可以以string.ToFormat的形式格式化字符串,使用非常方便
using System; public class Example { public static string ExampleString = "Hello {0}".ToFormat("you!"); } /// <summary> /// Helper function to format a string without having to type 'string.format(...' /// </summary> public static class StringExtensionMethods { public static string ToFormat(this string s, object arg0) { return string.Format(s, arg0); } public static string ToFormat(this string s, object arg0, object arg1) { return string.Format(s, arg0, arg1); } public static string ToFormat(this string s, object arg0, object arg1, object arg2) { return string.Format(s, arg0, arg1, arg2); } public static string ToFormat(this string s, params object[] args) { return string.Format(s, args); } public static string ToFormat(this string s, IFormatProvider provider, string format, params object[] args) { return string.Format(provider, s, args); } }