The main function of this article is to use asp.net to process Chinese string truncation and the excess part contains ellipsis. This is also common, but sometimes it is better to use css to process it.
The Code is as follows: |
Copy code |
/// <Summary> /// Cut the specified string by the specified length /// </Summary> /// <Param name = "oldStr"> string to be cut </param> /// <Param name = "maxLength"> Maximum length of the string </param> /// <Param name = "endWith"> suffix that exceeds the length </param> /// <Returns> If the length is exceeded, the intercepted string is added with a suffix; otherwise, the original string is returned. </returns> Public static string StringTruncate (string oldStr, int maxLength, string endWith) { If (string. IsNullOrEmpty (oldStr )) Return oldStr + endWith; If (maxLength <1) Throw new Exception ("the returned string length must be greater than [0]"); If (oldStr. Length> maxLength) { String strTmp = oldStr. Substring (0, maxLength ); If (string. IsNullOrEmpty (endWith )) Return oldStr; Else Return oldStr + endWith; } Return oldStr; } } |