c# – 如何将ToTitle函数更改为字符串扩展名?
发布时间:2020-12-16 00:01:49 所属栏目:百科 来源:网络整理
导读:我最近看到一篇帖子询问是否有办法更改字符串,因此它以大写字母开头并且后面有小写字母.这看起来是最好的解决方案: public static class StringHelper { public static string ToTitleCase(string text) { return StringHelper.ToTitleCase(text,CultureInf
我最近看到一篇帖子询问是否有办法更改字符串,因此它以大写字母开头并且后面有小写字母.这看起来是最好的解决方案:
public static class StringHelper { public static string ToTitleCase(string text) { return StringHelper.ToTitleCase(text,CultureInfo.InvariantCulture); } public static string ToTitleCase(string text,CultureInfo cultureInfo) { if (string.IsNullOrEmpty(text)) return string.Empty; TextInfo textInfo = cultureInfo.TextInfo; return textInfo.ToTitleCase(text.ToLower()); } } 我想要的是将这些转换为字符串扩展.有人可以建议我怎么做吗? 解决方法
在第一个参数前面使用this关键字:
public static class StringHelper { public static string ToTitleCase(this string text) { return StringHelper.ToTitleCase(text,CultureInfo.InvariantCulture); } public static string ToTitleCase(this string text,CultureInfo cultureInfo) { if (string.IsNullOrEmpty(text)) return string.Empty; TextInfo textInfo = cultureInfo.TextInfo; return textInfo.ToTitleCase(text.ToLower()); } } 请参阅MSDN上的How to: Implement and Call a Custom Extension Method (C# Programming Guide). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |