Considering that many browsers are not compatible with CSS, text-overflow is used to crop strings with "... ", there are also solutions to provide full compatibility on the Internet, but there is no direct way to process strings in the background.
Next I will provide the background processingCode:
/// <Summary> /// provide string operations /// </Summary> Public static class stringhelper {/// <summary> /// cut the string according to the specified length/ /// </Summary> /// <Param name = "originalstring"> original string </param> /// <Param name = "length"> cut length </param> /// <returns> the cropped string </returns> Public static string cropstring (string originalstring, int length) {If (length <originalstring. length) {return originalstring. substring (0, length) + "... ";} else Return originalstring ;} /// <summary> /// you can specify the length of the cropped string. // </Summary> /// <Param name = "originalstring"> original string </ param> /// <Param name = "length"> cut length </param> /// <Param name = "isseparate"> whether to distinguish Chinese and English </param> /// <returns> cropped string </returns> Public static string cropstring (string originalstring, int length, bool isseparate) {If (! Isseparate) {return cropstring (originalstring, length);} else {int Len = length; For (INT I = 0; I <(length> originalstring. length? Originalstring. Length: length); I ++) {RegEx RX = new RegEx ("^ [\ u4e00-\ u9fa5] $"); If (! Rx. ismatch (originalstring [I]. tostring () {Len ++;} return cropstring (originalstring, Len );}}}
The reason why the English and Chinese characters are reloaded is that even if the length is the same, the display width is not the same. Generally, the width of a Chinese character is about twice that of an English character, except for non-width fonts.
If Chinese and English characters are distinguished, the length string width displayed is almost the same.
Logic:
- Traverses the entire string;
- Extract each character to determine whether it is a Chinese character;
- If not, add 1 to the intercepted length; otherwise, continue the traversal;
- The loop ends and the string is cropped according to the new length;