c# string 扩展方法
场景:只显示一字符串的前50个字符,多余的用“...”省略号替代
如果不用扩展方法当然也可以实现,写一个静态方法,如下:
- public class StringUtil
- {
- /// <summary>
- /// 截取字符串
- /// </summary>
- /// <param name="str">要截取的字符串</param>
- /// <param name="length">截取长度</param>
- /// <returns></returns>
- public static string CutString(string str, int length)
- {
- if (!string.IsNullOrEmpty(str))
- {
- if (str.Length <= length)
- {
- return str;
- }
- else
- {
- return str.Substring(, length) + "...";
- }
- }
- else
- {
- return "";
- }
- }
- }
不用扩展方法
调用:
string result=StringUtil.CutString(str,50);
使用扩展方法:
使用扩展方法要注意两个点:1)扩展方法必须定义在静态类中,2)使用this关键字
- public static class StringExtension
- {
- /// <summary>
- /// 截取字符串,多余部分用"..."代替
- /// </summary>
- /// <param name="str">源字符串</param>
- /// <param name="length">截取长度</param>
- /// <returns></returns>
- public static string CutString(this string str, int length)
- {
- if (!string.IsNullOrEmpty(str))
- {
- if (str.Length <= length)
- {
- return str;
- }
- else
- {
- return str.Substring(, length) + "...";
- }
- }
- else
- {
- return "";
- }
- }
- }
扩展方法
调用:
string result=str.CutString(50);
这样是不是方便一些,代码简洁一些,就像你去调用ToString(),Substring()等系统定义的方法一样。
c# string 扩展方法的更多相关文章
- 扩展方法 1 简单的string扩展方法
这里是关于 String的简单扩展方法 (静态类 静态方法 this 类型 这里是string) static class Program { static void Main(string[] ar ...
- JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法
类似C#中的 PadLeft.PadRight方法 //方法一 function FillZero(p) { return new Array(3 - (p + '').length + 1).joi ...
- C#扩展方法知多少
前言:上篇 序列化效率比拼——谁是最后的赢家Newtonsoft.Json 介绍了下序列化方面的知识.看过Demo的朋友可能注意到了里面就用到过泛型的扩展方法,本篇打算总结下C#扩展方法的用法.博主打 ...
- C#扩展方法学习笔记
C#扩展方法,简单的理解是不修改原来类的源代码的情况下,为某个类添加某个方法.扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的.它们的第一个参数指定该方法作用于哪个类型,并且该参数以 th ...
- 再谈扩展方法,从string.IsNullOrEmpty()说起
string.IsNullOrEmpty()这个方法算得上是.net中使用频率最高的方法之一.此方法是string的一个静态方法,类似的静态方法在string这个类中还有很多.那么这样的方法作为静态方 ...
- ASP.Net string 类的扩展方法 [转]
string 类的扩展方法列表(基本相同于 IEnumerable<T> 接口的成员列表): Aggregate<> //累加 All<> / ...
- c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)
下面是 Queryable 类 中最常用的两个排序的扩展方法: 1 2 public static IOrderedQueryable<TSource> OrderBy<TSourc ...
- 给 string 添加一个 GetInputStream 扩展方法
有时候,我们须要读取一些数据,而无论这数据来源于磁盘上的数据文件,还是来源于网络上的数据.于是.就有了以下的 StringExtensions.cs: using System; using Syst ...
- 给string定义一个扩展方法
创建一个 static 的类,并且里面的方法也必须是static的,第一个参数是被扩展的对象,必须标注为this,使用时,必须保证namespace using进来了. 实例: using Syste ...
随机推荐
- 【css】css 中文字体 unicode 对照表
css 中文字体可以用 unicode 格式来表示,比如“宋体”可以用 \5B8B\4F53 来表示.具体参考下表: 中文名 英文名 unicode 宋体 SimSun \5B8B\4F53 黑体 S ...
- Java8学习笔记(三)--方法引入
基本概念 格式 实例变量名 | 类名 :: 静态方法 | 实例方法 作用 简化Lambda表达式 示例 a -> System.out.println(a); <=> System. ...
- java指纹识别+谷歌图片识别技术_源代码
主类: import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; public c ...
- 前端分页插件pagination
摘要: 最近在开发项目中又用到了前端分页,以前也做过,为了方便以后使用所以将他封装成第三方插件,不依赖任何库.网上已经有很多插件,问什么还要自己造轮子? 自己写的扩展性高 不依赖任何库 作为一次技术沉 ...
- python concurrent.futures.Threadpoolexcutor的有界队列和无界队列
1.默认是无界队列,如果生产任务的速度大大超过消费的速度,则会把生产任务无限添加到无界队列中,这样一来控制不了生产速度,二来是会造成系统内存会被队列中的元素堆积增多而耗尽. 2.改写为有界队列 cla ...
- 【Oracle】删除所有表
BEGIN FOR cur_rec IN (SELECT object_name, object_type FROM user_objects WHERE object_type IN ('TABLE ...
- 【Spring】Spring中用到的设计模式
1.简单工厂 又叫静态工厂方法模式,不属于23种设计模式之一. 简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类. Spring中的BeanFactory就是简单工厂模式的 ...
- [转]Mariadb的root密码忘记后的解决方法
环境背景:CentOS 7.2 一.编辑/usr/lib/systemd/system/mariadb.service 文件,在Service段中添加 1 2 3 4 5 6 7 8 9 10 ...
- POJ 1958 Strange Towers of Hanoi
Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3784 Accepted: 23 ...
- day_5.07py
正则: