Intercepts a fixed-length string displayed on the page with an extra part displayed as an ellipsis (distinguishing Chinese characters from characters)

Source: Internet
Author: User
Tags return string
Chinese Character | display | page | string | String The following is code, OH, relatively simple, mainly to distinguish between Chinese characters and letters, or a whole is a letter, a full of Chinese characters in the two records will be more ugly, the length of the whole character only half of the whole Chinese characters show ... Number.
public static string StringFormat (String str,int N)
{
///
format string length, out of partial display ellipsis, distinguishing characters from letters. Kanji 2 bytes, alphanumeric one byte
///
String temp=string. Empty;
if (System.Text.Encoding.Default.GetByteCount (str) <=n)//If the length is smaller than the desired length n, return the original string
{
return str;
}
Else
{
int t=0;
Char[] Q=str. ToCharArray ();
for (int i=0;i<q.length&&t<n;i++)
{
if (int) q[i]>=0x4e00 && (int) q[i]<=0x9fa5)//whether Chinese characters
{
Temp+=q[i];
t+=2;
}
Else
{
Temp+=q[i];



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.