C#截取指定长度字符超出部分以"..."代替,用于处理过长的标题

public static string StringTruncat(string oldStr, int maxLength, string endWith)
{
    // 判断原字符串是否为空
    if (string.IsNullOrEmpty(oldStr))
        return oldStr + endWith;
 
 
    // 返回字符串的长度必须大于 1
    if (maxLength < 1)
        throw new Exception("返回的字符串长度必须大于 [0] ");
 
 
    // 判断原字符串是否大于最大长度
    if (oldStr.Length > maxLength)
    {
        // 截取原字符串
        string strTmp = oldStr.Substring(0, maxLength);
 
 
        // 判断后缀是否为空
        if (string.IsNullOrEmpty(endWith))
            return strTmp;
        else
            return strTmp + endWith;
    }
    return oldStr;
} 

  

原文地址:https://www.cnblogs.com/xssxss/p/2722894.html