<%
'判断包含双字节字符的字符串长度
Function getStrLen(str)
dim x,y,i
x = len(str)
y = 0
for i = 1 to x
if asc(mid(str,i,1))<0 or asc(mid(str,i,1))>255 then
y = y + 2 '双字节
else
y = y + 1'ascii码字符
end if
next
getStrLen = y
End Function
'从指定(包含双字节字符的)字符串中第一个字符起的指定长度的字符串
'指定的长度为单字节长度,即字节数
Function leftString(str,length)
dim x,y,i,s
s=str
x = len(str)
y = 0
if x >= 1 then
for i = 1 to x
if asc(mid(str,i,1)) < 0 or asc(mid(str,i,1)) >255 then
y = y + 2
else
y = y + 1
end if
if y >= length then
s = left(str,i) '字符串字节数
exit for
end if
next
end if
leftString = s
End Function %>
<%=getStrLen("双字节字符abc")%> <br>
<%=leftString("abc双字节字符字符串",10)%>
本文提供了一种在经典脚本环境中处理包含双字节字符的字符串的方法,包括如何计算其真实长度及如何按指定字节数截取字符串。这对于处理中文等双字节字符的语言非常有用。

被折叠的 条评论
为什么被折叠?



