Cropping long strings (background Mode)

Source: Internet
Author: User

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:

    1. Traverses the entire string;
    2. Extract each character to determine whether it is a Chinese character;
    3. If not, add 1 to the intercepted length; otherwise, continue the traversal;
    4. The loop ends and the string is cropped according to the new length;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.