asp string interception function
'************************************************ **********
'Function: cutStr[str(strlen)]
'Parameters: str, string to be processed, strlen, intercepted length
'Author: Mumu
'Date: 2007/7/12
'Description: intercept a string of specified length
'Example: <%=cutStr("Welcome to Alixixi",5)%>
'************************************************ **********
function cutStr(str,strlen)
If str = "" Then
cutStr = "cutStr function exception: string is empty"
exit function
End If
'------------Source length check
If strlen = "" Then
cutStr = "cutStr function exception: length not specified"
exit function
End If
If CInt(strlen) = 0 Then
cutStr = "cutStr function exception: length is 0"
exit function
End If
'----------Detect source character length
dim l,t,c,i
l=len(str)
t=0
'----------Loop to intercept characters
for i=1 to l
c=Abs(Asc(Mid(str,i,1)))
'------Determine whether it is a Chinese character
if c>255 then
t=t+2
else
t=t+1
endIf
'------Determine whether the specified length is reached
if t>=strlen then
cutStr=left(str,i)&".."
exit for
else
cutStr=str
end if
next
cutStr=replace(cutStr,chr(10),"")
end function
''************************************************ *********
'Function: strlen[str]
'Parameter: str, string to be processed
'Author: Mumu
'Date: 2007/7/12
'Description: Determine the length of the string, the length of Chinese characters is 2
'Example: <%=strlen("Welcome to Alixixi")%>
'************************************************ **********
Function strlen(str)
dim p_len
p_len=0
strlen=0
if trim(str)<>"" then
p_len=len(trim(str))
for xx=1 to p_len
if asc(mid(str,xx,1))<0 then
strlen=int(strlen) + 2
else
strlen=int(strlen) + 1
end if
next
end if
End Function
Intercept the n characters on the left '******************************************* ****************
'Function: LeftTrue(str,n)
'Parameters: str, string to be processed, n, intercepted length
'Author: Mumu
'Date: 2007/7/12
'Description: Display the n characters on the left (automatic recognition of Chinese characters) function
'Example: <%=LeftTrue("Welcome to Alixixi",6)%>
'************************************************ **********
Function LeftTrue(str,n)
If len(str)<=n/2 Then
LeftTrue=str
Else
Dim TStr
Dim l,t,c
Dim i
l=len(str)
t=l
TStr=""
t=0
for i=1 to l
c=asc(mid(str,i,1))
If c<0 then c=c+65536
If c>255 then
t=t+2
Else
t=t+1
End If
If t>n Then exit for
TStr=TStr&(mid(str,i,1))
next
LeftTrue = TStr
End If
End Function
Articles that may interest you: