Function StrLength(txt) '计算字符串长度,英文字母为1,汉字为2
dim x,y,ii
txt=trim(txt)
x = len(txt)
y = 0
for ii = 1 to x
if asc(mid(txt,ii,1)) < 0 or asc(mid(txt,ii,1)) >255 then '如果是汉字
y = y + 2
else
y = y + 1
end if
next
StrLength = y
End Function
function bytelen(HTMLS) '计算字符串的字节长度
Dim regEx ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = "[^\x00-\xff]" ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分字符大小写。
regEx.Global = True ' 设置全局可用性。
bytelen = len(regEx.Replace(HTMLS,"xx")) ' 执行搜索替换得到字节长度。
end Function
function byteleft(str,length) '按字节长度左截字符串
Dim regEx,tmpch,curlen,i ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = "[^\x00-\xff]" ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分字符大小写。
regEx.Global = True ' 设置全局可用性。
if bytelen(str)>length then
byteleft=""
curlen=0
i=0
while curlen<length
i=i+1
tmpch=mid(str,i,1)
byteleft=byteleft+tmpch
curlen=curlen+len(regEx.Replace(tmpch,"xx"))
wend
else
byteleft=str
end if
end Function
<script language="javascript">
function strlen(str)
...{ var len; var i; len = 0;
for (i=0;i<str.length;i++)
...{ if (str.charCodeAt(i)>255) len+=2; else len++; }
alert("长度为:"+len); }