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 + " ... " );
}
}