asp字符串截取函数
'*********************************************************
'函数:cutStr[str(strlen)]
'参数:str,待处理的字符串,strlen,截取的长度
'作者:木木
'日期:2007/7/12
'描述:截取指定长度的字符串
'示例:<%=cutStr("欢迎光临阿里西西",5)%>
'*********************************************************
functioncutStr(str,strlen)
Ifstr=""Then
cutStr="cutStr函数异常:字符串为空"
exitfunction
EndIf
'------------来源长度检查
Ifstrlen=""Then
cutStr="cutStr函数异常:长度未指定"
exitfunction
EndIf
IfCInt(strlen)=0Then
cutStr="cutStr函数异常:长度为0"
exitfunction
EndIf
'----------检测来源字符长度
diml,t,c,i
l=len(str)
t=0
'----------循环截取字符
fori=1tol
c=Abs(Asc(Mid(str,i,1)))
'------判断是否汉字
ifc>255then
t=t+2
else
t=t+1
endIf
'------判断是否到达指定长度
ift>=strlenthen
cutStr=left(str,i)&".."
exitfor
else
cutStr=str
endif
next
cutStr=replace(cutStr,chr(10),"")
endfunction
''*********************************************************
'函数:strlen[str]
'参数:str,待处理的字符串
'作者:木木
'日期:2007/7/12
'描述:判断字符串长度,汉字长度为2
'示例:<%=strlen("欢迎光临阿里西西")%>
'*********************************************************
Functionstrlen(str)
dimp_len
p_len=0
strlen=0
iftrim(str)<>""then
p_len=len(trim(str))
forxx=1top_len
ifasc(mid(str,xx,1))<0then
strlen=int(strlen)+2
else
strlen=int(strlen)+1
endif
next
endif
EndFunction
截取左边的n个字符'*********************************************************
'函数:LeftTrue(str,n)
'参数:str,待处理的字符串,n,截取的长度
'作者:木木
'日期:2007/7/12
'描述:显示左边的n个字符(自动识别汉字)函数
'示例:<%=LeftTrue("欢迎光临阿里西西",6)%>
'*********************************************************
FunctionLeftTrue(str,n)
Iflen(str)<=n/2Then
LeftTrue=str
Else
DimTStr
Diml,t,c
Dimi
l=len(str)
t=l
TStr=""
t=0
fori=1tol
c=asc(mid(str,i,1))
Ifc<0thenc=c+65536
Ifc>255then
t=t+2
Else
t=t+1
EndIf
Ift>nThenexitfor
TStr=TStr&(mid(str,i,1))
next
LeftTrue=TStr
EndIf
EndFunction