zoukankan      html  css  js  c++  java
  • .NET截取指定长度汉字超出部分以"..."代替

    1. ///   <summary>    
    2.    ///   将指定字符串按指定长度进行剪切,    
    3.    ///   </summary>    
    4.    ///   <param   name= "oldStr "> 需要截断的字符串 </param>    
    5.    ///   <param   name= "maxLength "> 字符串的最大长度 </param>    
    6.    ///   <param   name= "endWith "> 超过长度的后缀 </param>    
    7.    ///   <returns> 如果超过长度,返回截断后的新字符串加上后缀,否则,返回原字符串 </returns>    
    8.    public static string StringTruncat(string oldStr, int maxLength, string endWith)   
    9.    {   
    10.        if (string.IsNullOrEmpty(oldStr))   
    11.            //   throw   new   NullReferenceException( "原字符串不能为空 ");    
    12.            return oldStr + endWith;   
    13.        if (maxLength < 1)   
    14.            throw new Exception("返回的字符串长度必须大于[0] ");   
    15.        if (oldStr.Length > maxLength)   
    16.        {   
    17.            string strTmp = oldStr.Substring(0, maxLength);   
    18.            if (string.IsNullOrEmpty(endWith))   
    19.                return strTmp;   
    20.            else  
    21.                return strTmp + endWith;   
    22.        }   
    23.        return oldStr;   
    24.    }   
  • 相关阅读:
    codeforces570D Tree Requests
    codeforces600E Lomsat gelral
    BZOJ2001 [Hnoi2010]City 城市建设
    BZOJ2565 最长双回文串
    BZOJ4031 [HEOI2015]小Z的房间
    BZOJ2467 [中山市选2010]生成树
    SPOJ104 HIGH
    爆零系列—补题A
    DP一直是自己的弱势 开始练滚动数组——HDOJ4502
    HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1891993.html
Copyright © 2011-2022 走看看