NET intercepts the excess part of a character of specified length to '... 'Instead of instance sharing

  • 2020-06-15 08:00:25
  • OfStack


///   <summary>
    ///    Cuts the specified string to the specified length, 
    ///   </summary>
    ///   <param   name= "oldStr ">  String to truncate  </param>
    ///   <param   name= "maxLength ">  The maximum length of a string  </param>
    ///   <param   name= "endWith ">  Suffixes that exceed length  </param>
    ///   <returns>  If the length is exceeded, the truncated new string is returned with the suffix; otherwise, the original string is returned  </returns>
    public static string StringTruncat(string oldStr, int maxLength, string endWith)
    {
        if (string.IsNullOrEmpty(oldStr))
            //   throw   new   NullReferenceException( " The original string cannot be empty  ");
            return oldStr + endWith;
        if (maxLength < 1)
            throw new Exception(" The length of the string returned must be greater than [0] ");
        if (oldStr.Length > maxLength)
        {
            string strTmp = oldStr.Substring(0, maxLength);
            if (string.IsNullOrEmpty(endWith))
                return strTmp;
            else
                return strTmp + endWith;
        }
        return oldStr;
    }


Related articles: