场景:只显示一字符串的前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. 扩展方法 1 简单的string扩展方法

    这里是关于 String的简单扩展方法 (静态类 静态方法 this 类型 这里是string) static class Program { static void Main(string[] ar ...

  2. JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法

    类似C#中的 PadLeft.PadRight方法 //方法一 function FillZero(p) { return new Array(3 - (p + '').length + 1).joi ...

  3. C#扩展方法知多少

    前言:上篇 序列化效率比拼——谁是最后的赢家Newtonsoft.Json 介绍了下序列化方面的知识.看过Demo的朋友可能注意到了里面就用到过泛型的扩展方法,本篇打算总结下C#扩展方法的用法.博主打 ...

  4. C#扩展方法学习笔记

    C#扩展方法,简单的理解是不修改原来类的源代码的情况下,为某个类添加某个方法.扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的.它们的第一个参数指定该方法作用于哪个类型,并且该参数以 th ...

  5. 再谈扩展方法,从string.IsNullOrEmpty()说起

    string.IsNullOrEmpty()这个方法算得上是.net中使用频率最高的方法之一.此方法是string的一个静态方法,类似的静态方法在string这个类中还有很多.那么这样的方法作为静态方 ...

  6. ASP.Net string 类的扩展方法 [转]

    string 类的扩展方法列表(基本相同于 IEnumerable<T> 接口的成员列表): Aggregate<>     //累加 All<>        / ...

  7. c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)

    下面是 Queryable 类 中最常用的两个排序的扩展方法: 1 2 public static IOrderedQueryable<TSource> OrderBy<TSourc ...

  8. 给 string 添加一个 GetInputStream 扩展方法

    有时候,我们须要读取一些数据,而无论这数据来源于磁盘上的数据文件,还是来源于网络上的数据.于是.就有了以下的 StringExtensions.cs: using System; using Syst ...

  9. 给string定义一个扩展方法

    创建一个 static 的类,并且里面的方法也必须是static的,第一个参数是被扩展的对象,必须标注为this,使用时,必须保证namespace using进来了. 实例: using Syste ...

随机推荐

  1. snopy 数据库简介

    snoRNA ,small nucleolar RNA, 核仁小分子RNA, 是一类在细胞核内的small non-coding RNA, 长度在60-300nt, 其主要功能是调控其他小RNA分子( ...

  2. JS中lambda表达式的优缺点和使用场景(转)

    add by zhj: 最近在看ES6,看到了箭头函数,我个人感觉箭头函数适用于函数体中不用this的匿名函数,在箭头函数中使用this是一个坑 原文:http://ourjs.com/detail/ ...

  3. Html 助手

    /*! * Html 助手 * version: 1.0.0-2018.07.25 * Requires ES6 * Copyright (c) 2018 Tiac * http://www.cnbl ...

  4. Oracle中add_months()函数的用法

    查询当前时间1个月以前的时间: select add_months(sysdate,-1) from dual; 查询当前时间1个月以后的时间: select add_months(sysdate,1 ...

  5. scrapy爬取某网站,模拟登陆过程中遇到的那些坑

    本节内容 在访问网站的时候,我们经常遇到有些页面必须用户登录才能访问.这个时候我们之前写的傻傻的爬虫就被ban在门外了.所以本节,我们给爬虫配置cookie,使得爬虫能保持用户已登录的状态,达到获得那 ...

  6. git 删除仓库的文件

    git移除远程仓库某个文件夹 1.比如src/product/ 文件夹 git rm -r --cached "src/product" //执行命令. 2.提交到本地 git c ...

  7. windows下模拟网络延时、丢包、抖动

    1.Fiddler 免费软件 模拟网速功能比较单一(Rules --> Performance --> Simulate Modem speed),选项较少,Fiddler仅是减缓带宽并未 ...

  8. spring整合Jersey 无法注入service的问题

    现象: action中的@autowired注入service或dao失败,报空指针异常 原因: 造成该问题的原因是你并没有做好spring和jersey的整合工作,检查你的web.xml文件,jer ...

  9. (广度搜索)A - Prime Path(11.1.1)

    A - Prime Path(11.1.1) Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64 ...

  10. 洛谷P1029 最小公约数和最大公倍数问题【数论】

    题目:https://www.luogu.org/problemnew/show/P1029 题意: 给定两个数$x$和$y$,问能找到多少对数$P$$Q$,使得他们的最小公约数是$x$最大公倍数是$ ...