ASP text truncation functions compatible with UTF-8 and GBK Encoding

Source: Internet
Author: User

Bubble network tutorial [http://www.hugo8.com]

I recently searched for the function of text Truncation on the Internet and found that the following code is very likely to appear, so I tested it.

The following code is from the network: '*************************************
'Cut content-separate by character
'Chinese is counted as 2 Characters
'*************************************
Function cutstr (byval STR, byval strlen)
Dim L, T, C, I
If isnull (STR) Then cutstr = "": Exit Function
L = Len (STR)
Strlen = int (strlen)
T = 0
For I = 1 to L
C = ASC (mid (STR, I, 1 ))
If C> = 0 and C <= 255 then t = t + 1 else t = T + 2
If T> strlen then
Cutstr = left (STR, I-1 )&"..."
Exit
Else
Cutstr = Str
End if
Next
End Function

This function cannot be used when converting the entire site to UTF-8 encoding. Because it is obvious that his judgment method is if ASC (mid (STR, X, 1) <0 then, rather than the ASCII code page, the ASC function is useless. So I found the relevant information and finally found the solution:

ASC returns the code data point or character code of the input character. For single-byte character sets (sbcs), the return value range is 0 to 255. For double-byte character sets (DBCS), the return value range is-32768 to 32767. The returned value depends on the code page of the current thread. The code page is included in the ansicodepage attribute of the textinfo class. You can obtain the textinfo. ansicodepage by specifying system. Globalization. cultureinfo. currentculture. textinfo. ansicodepage.

ASCW returns the Unicode code data point of the input character. The return value ranges from 0 to 65535. The returned value is irrelevant to the current thread's culture and code page settings.

Note that for bytes, The ASCB function in earlier versions of Visual Basic returns code rather than characters. It is mainly used to convert strings in the dual-byte character set (DBCS) application. All Visual Basic. Net strings are in unicode format and no longer support ASCB.

That is to say, in UTF-8 encoding format, you need to use ASCW to "recognize" Chinese and English!

NowCutstrFunction

The following is a reference clip:
C = ASC (mid (STR, I, 1 ))

Change

The following is a reference clip:
C = ASCW (mid (STR, I, 1 ))

The results are as expected!

This is a list of articles on the sidebar of this website. The custom length is truncated. The Chinese character is two characters!

Knowledge about ASC functions:

The following is a reference clip:
ASC Functions

Returns the ANSI character code corresponding to the first letter of the string.

ASC (string)
The string parameter is any valid string expression. If the string parameter does not contain characters, a runtime error occurs.

Description
In the following example, ASC returns the ANSI character code of the first letter of each string:

Dim mynumber
Mynumber = ASC ("A") ''returns 65.
Mynumber = ASC ("A") ''returns 97.
Mynumber = ASC ("apple") ''returns 65.

Note that the ASCB function is used together with a string containing byte data. ASCB does not return the first character code, but returns the first byte. ASCW is provided for 32-bit platforms that use Unicode characters. It returns the Unicode (WIDE) character code, so it can avoid Code Conversion from ANSI to Unicode.

Original article reproduced, please indicate the source: Bubble network tutorial [http://www.hugo8.com]
Link: http://www.hugo8.com/article.asp? Id = 118

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.