Two Methods for capturing strings in a mix of Chinese and English

Source: Internet
Author: User

Method 1: Use the c # program for processing.

Code attachment

View Code

/// <Summary> /// return the mixed length of a string /// </summary> /// <param name = "sourceStr"> source string </param>/ // <returns> </returns> public static int MixLength (this string sourceStr) {int mixLength = sourceStr. length; for (int I = 0; I <sourceStr. length; I ++) {byte [] bytes = Encoding. default. getBytes (sourceStr. substring (I, 1); if (bytes. length> 1) {mixLength ++;} return mixLength ;} /// <summary> // intercept a mix of Chinese and English strings equivalent to mixLength. // </summary> /// <param name = "sourceStr"> source string </param> // <param name = "mixLength"> specifies the length to be truncated, calculate the length unit of two Chinese characters </param> // <returns> </returns> public static string MixSubstring (this string sourceStr, int mixLength) {int length = mixLength; bool flag = false; // indicates whether the length is reduced by 1 int I = 0; while (I <length) {if (flag) {length --; flag = false; // reset} byte [] bytes = Encoding. default. getBytes (sourceStr. substring (I, 1); if (bytes. length> 1) // if it is Chinese, the next time you subtract one {flag = true;} I ++;} return sourceStr. substring (0, length );}

Disadvantage: Chinese characters are not twice the length of English characters on the displayed interface.

Method 2: Use css to control the style.

If the content of td in the control table is omitted, add the style table-layout: fixed to the table first;

Then add the style to the corresponding td:

 

. Ellipsis {width: 200px; // The white-space: nowrap; word-break: keep-all; overflow: hidden; text-overflow: ellipsis ;}

 

If it is a span or div, apply the style directly.

Disadvantage: there is a slight difference between different browsers, but it is very accurate compared to the first method.

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.