ASP too long title truncation, month/day Truncation

Source: Internet
Author: User
Method 1:
In the document list, if the title is too long, it will often break through the form to damage the page image. The general practice is to skip the extra-long part and replace it with a ellipsis. For example, to take the first 10 characters, you can write the following statement:

If Len (title)> 10 then Title = left (title, 9) + "... "
'The character is exactly 10 characters in length.

Chinese people face the reality that the Chinese character width is twice the letter width. Therefore, we need to design a function to calculate the length of a string using a variable. If a letter is encountered, the length is increased by 1. If a Chinese character is encountered, the length is increased by 2:

Function cuttitle (STR, strlen)
'Str is the title to be split, and strlen is the truncation length (calculated by letters)
Dim tmplen, tmpstr, I, S
Tmpstr = ""
Tmplen = 0
STR = trim (STR)
If STR = "" Then exit function
For I = 1 to Len (STR)
S = mid (STR, I, 1)
Tmpstr = tmpstr & S
Tmplen = tmplen + 1
If ASC (s) <0 then tmplen = tmplen + 1
'If it is a Chinese character, the length is increased by 1
If tmplen> strlen then
Tmpstr = left (tmpstr, Len (tmpstr)-2 )&"... "
Exit
End if
Next
Cuttitle = tmpstr
End Function
----------------------------------------------------------------------------
Method 2:
<%
Function getlen (STR) 'checks the length of a string containing Chinese characters. The length of a Chinese character is 2, and the length of an English letter is 1.
Dim strlen, I
Getlen = 0
Strlen = Len (STR)
If isnull (STR) Then exit function end if
For I = 1 to strlen
If ASC (mid (STR, I, 1) <0 then
Getlen = getlen + 2
Else
Getlen = getlen + 1
End if
Next
End Function

Function getleft (STR, L, Alter) 'gets the length of the string and crops it according to requirements. The length is calculated based on the length of the Chinese character. The length of one Chinese Character = the length of two English letters, str indicates the string variable or constant to be truncated, L indicates the length to be retained after truncation, and alter indicates the character to be truncated and supplemented, which is generally ".... ".
Dim I, j
J = 1
Getleft = ""
If getlen (STR)> 2 * l then
For I = 1 to L
If ASC (mid (STR, J, 1) <0 or ASC (mid (STR, J + 1, 2) <0 then
Getleft = getleft & Mid (STR, J, 1)
J = J + 1
Else
Getleft = getleft & Mid (STR, J, 2)
J = J + 2
End if
Next
If alter <> "" then
Getleft = getleft & alter
End if
Else
Getleft = Str
End if
End Function
%>
Specifically, the function getlen (STR) function calculates the length of a string. My calculation rule is that the length of a Chinese character is 2, and the length of an English letter is 1.
Function getleft (STR, L, Alter) is a truncated function.
For example, to truncate the string "code test" to keep only the "code", and then add "...", you only need
<%
Getleft ("code test", 2 ,"....")
%>
---------------------------------------------------------------------------------
Method 3: (intercept Chinese and English strings)
<%
Function strleft (STR, num)
Dim p_str, p_num
P_str = ""
P_num = 0
If trim (STR) <> "then
P_len = Len (STR)
For I = 1 to p_len
If ASC (mid (STR, I, 1)> 255 or ASC (mid (STR, I, 1) <0 then
P_num = p_num + 2
Else
P_num = p_num + 1
End if

If p_num> num then
P_str = left (STR, I-1)
Exit
Else
P_str = Str
End if
Next
End if
Strleft = p_str
End Function
%>
______________________________________________

Screenshot month/day:
<% = Month (now () %>-<% = Day (now () %>

Function date_md (Tim)
Dim M, d
If month (Tim)> 9 then
M = month (Tim)
Else
M = "0" & month (Tim)
End if
If Day (Tim)> 9 then
D = Day (Tim)
Else
D = "0" & Day (Tim)
End if
Date_md = M & "-" & D
End Function

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.