Asp.net: truncate a fixed-length string and display it on the page. The excess part is shown as a ellipsis.

Source: Internet
Author: User
Asp.net: truncate a fixed-length string and display it on the page. The excess part is shown as a ellipsis.

Below isCodeHaha, it's relatively simple. It's mainly about distinguishing Chinese characters and letters. Otherwise, it's hard to see if two records of all Chinese characters are arranged together, the length of a full character is only half of that of a Chinese character... no.

 

Public Static String Stringformat ( String STR, Int N)
{
///
/// The length of the formatted string. The ellipsis (...) is displayed for the part exceeding the limit, which distinguishes Chinese characters from letters. Two Chinese characters, one letter or Digit
///
String Temp = String . Empty;
If (System. Text. encoding. Default. getbytecount (STR) <= N) // If the length is less than the required length N, the original string is returned.
{
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 ) // Chinese character or not
{
Temp + = Q [I];
T + = 2 ;
}
Else
{
Temp + = Q [I];
T ++;
}
}
Return (Temp + " ... " );
}
}

 

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.