[C#] C#扩展String类,自定义ToFormat方法格式化字符串 →→→→→进入此内容的聊天室

来自 , 2021-03-21, 写在 C#, 查看 136 次.
URL http://www.code666.cn/view/1e65040d
  1. using System;
  2.  
  3. public class Example
  4. {
  5.     public static string ExampleString = "Hello {0}".ToFormat("you!");
  6. }
  7.  
  8. /// <summary>
  9. /// Helper function to format a string without having to type 'string.format(...'
  10. /// </summary>
  11. public static class StringExtensionMethods
  12. {
  13.     public static string ToFormat(this string s, object arg0)
  14.     {
  15.         return string.Format(s, arg0);
  16.     }
  17.  
  18.     public static string ToFormat(this string s, object arg0, object arg1)
  19.     {
  20.         return string.Format(s, arg0, arg1);
  21.     }
  22.  
  23.     public static string ToFormat(this string s, object arg0, object arg1, object arg2)
  24.     {
  25.         return string.Format(s, arg0, arg1, arg2);
  26.     }
  27.  
  28.     public static string ToFormat(this string s, params object[] args)
  29.     {
  30.         return string.Format(s, args);
  31.     }
  32.  
  33.     public static string ToFormat(this string s, IFormatProvider provider, string format, params object[] args)
  34.     {
  35.         return string.Format(provider, s, args);
  36.     }
  37.    
  38. }
  39. //csharp/8209

回复 "C#扩展String类,自定义ToFormat方法格式化字符串"

这儿你可以回复上面这条便签

captcha