Calculate the length of a Chinese/English string in ASP. NET

Source: Internet
Author: User

In typographical layout, we basically think that an English character occupies 1/2 of the width of Chinese characters, and Twitter contains 140 characters. In English, there are actually few words to write, the 140 Chinese Characters in Chinese can basically express a point of view clearly. Therefore, in some cases, it is required that English characters are calculated by half of Chinese characters. Therefore, you can use regular expressions to match whether each character is a Chinese character. Matching Based on different needs

[\ U4e00-\ u9fa5] matching simplified

[\ U4e00-\ u9fff] matches simplified and traditional

[\ U2e80-\ u9fff] matches all languages in East Asia.

 

/// <Summary> <br/> // check the length of both Chinese and English characters (0.5 for English characters and 1 for Chinese characters) <br/> // </Summary> <br/> // <Param name = "Source"> </param> <br/> // <returns> </returns> <br/> private float getstringlengthwithchinlish (string source) <br/>{< br/> RegEx r = new RegEx (@ "[\ u4e00-\ u9fa5]"); // Chinese <br/> float Len = 0.0f; <br/> char [] stringchar = source. tochararray (); <br/> foreach (char CHR in stringchar) {<br/> console. write (CHR. tostring (); <br/> If (R. ismatch (CHR. tostring () {<br/> Len + = 1; <br/>}else {<br/> Len + = 0.5f; <br/>}</P> <p> return Len; <br/>}< br/>

 

 

 

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.