left() 函數是 VBScript 的函數,VBScript 將1個漢字看作1個字符,因此用 l
2024-07-21 02:25:04
供稿:網友
left() 函數是 vbscript 的函數,vbscript 將1個漢字看作1個字符,因此用 left()不能得到正確的字符長度。
我自己編寫了如下3個函數,用來取代 len()、left()、right(),希望能解決您的問題。
'--------------------------------------------------------
'name: lenx
'argument: ustr
'return:
'description: 返回字符串的長度,1個中文字符長度為2
'--------------------------------------------------------
function lenx(byval ustr)
dim thelen,x,testustr
thelen = 0
for x = 1 to len(ustr)
testustr = mid(ustr,x,1)
if asc(testustr) < 0 then
thelen = thelen + 2
else
thelen = thelen + 1
end if
next
lenx = thelen
end function
'--------------------------------------------------------
'name: leftx
'argument: ustr 待處理的字符串
' ulen 要截取的長度
'return:
'description: 返回指定長度的字符串,1個中文字符長度為2
'--------------------------------------------------------
function leftx(byval ustr,byval ulen)
dim i,j,uteststr,thestr
leftx = ""
j = 0
for i = 1 to len(ustr)
uteststr= mid(ustr,i,1)
thestr = thestr & uteststr
if asc(uteststr) < 0 then
j = j + 2
else
j = j + 1
end if
if j >= ulen then exit for
next
leftx = thestr
end function
'--------------------------------------------------------
'name: rightx
'argument: ustr 待處理的字符串
' ulen 要截取的長度
'return:
'description: 返回指定長度的字符串,1個中文字符長度為2
'--------------------------------------------------------
function rightx(byval ustr,byval ulen)
dim i,j,uteststr
rightx = ""
j = 0
for i = len(ustr) to 1 step -1
uteststr = mid(ustr,i,1)
rightx = rightx & uteststr
if asc(uteststr) < 0 then
j = j + 2
else
j = j + 1
end if
if j >= ulen then exit for
next
end function
菜鳥學堂: