Net how to intercept the excess part of a specified length of Chinese characters and replace it with & quot;..."
This article mainly introduces how .NET intercepts the excess part of Chinese characters of specified length in order to "." Instead, there is a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
The code is as follows:
/ / /
/ / cut the specified string to the specified length
/ / /
/ / string to be truncated
The maximum length of / / string
/ / A suffix exceeding the length
/ / if the length is exceeded, the new truncated string is returned with a suffix, otherwise, the original string is returned.
Public static string StringTruncat (string oldStr, int maxLength, string endWith)
{
If (string.IsNullOrEmpty (oldStr))
/ / throw new NullReferenceException ("original string cannot be empty")
Return oldStr + endWith
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
}
Thank you for reading this article carefully. I hope the article ".NET how to intercept the excess part of a specified length of Chinese characters is helpful to everyone." at the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!