asp获取字符长度函数和截取字符串函数(可以测试1个汉字占两个字符长度)
本文章讲的是asp获取字符长度函数和截取字符串函数(可以测试1个汉字占两个字符长度),具体代码如下:
测试字符串长度
Function CheckStringLength(txt)
txt=trim(txt)
x=len(txt)
y=0
for ii=1to x
if asc(mid(txt,ii,1))<0or asc(mid(txt,ii,1))>255then'如果是汉字
y=y+2
else
y=y+1
end if
next
CheckStringLength=y
End Function
截取字符串
function InterceptString(txt,length)
txt=trim(txt)
x=len(txt)
y=0
if x>=1then
for ii=1to x
if asc(mid(txt,ii,1))<0or asc(mid(txt,ii,1))>255then'如果是汉字
y=y+2
else
y=y+1
end if
if y>=length then
txt=left(trim(txt),ii)'字符串限长
exit for
end if
next
InterceptString=txt
else
InterceptString=""
end if
End Function