扩展方法示例
扩展方法很是方便,以前写过,后来忘掉了,所以就写个博客防止以后忘记。
下为一个替换字符串的简单方法的示例:
public static class StringFormat
{
public static string StringFormat(this string str)
{
return str.Replace(" ", "").Replace("'", "‘").Replace(",", ",").TrimStart(' ').TrimEnd(' ').Trim();
}
}
1.类需要为静态类
2.需要为静态方法
3.参数需要加上this
下为调用方法:
string a = " String Format ";
Console.WriteLine(a.StringFormat());

浙公网安备 33010602011771号