|
 |
asp截取正确的英文/汉字长度 |
作者:admin 发表时间:2014/9/23 人气: |
|
|
<% '*************************************************** '函数名称: GetTrueLength(strChinese, lenMaxWord, strSpaceBar) '函数功能: 截取正确的英文/汉字长度 '参数说明: strChinese 为被检测字符串 ' lenMaxWord 为限制的字符长度 ' strSpaceBar 为要过滤(去掉)的字符 '返回 值 : 格式化的字符串(注:一个中文两个字符。) '*************************************************** Function GetTrueLength(strChinese, lenMaxWord, strSpaceBar) dim i, j, strTail, lenTotal, lenWord, lenNow dim strWord, bOverFlow, RetString if strChinese = "" or vartype(strChinese) = vbNull or CLng(lenMaxWord) <= 0 then GetTrueLength = "" exit function end if strTail = "..." '标题截取后的表示,如“…” bOverFlow = False lenTotal = 0 for i=1 to Len(strChinese) strWord = mid(strChinese, i, 1) if asc(strWord) < 0 or asc(strWord) > 127 then lenTotal = lenTotal + 2 else lenTotal = lenTotal + 1 end if next '判断字符是否溢出 if lenTotal > lenMaxWord then bOverFlow = True strSpaceBar = "" if bOverFlow = True then '字符溢出,去尾 lenWord = 0 RetString = "" for i=1 to Len(strChinese) strWord = mid(strChinese, i, 1) if asc(strWord) < 0 or asc(strWord) > 127 then lenNow = 2 else lenNow = 1 lenWord = lenWord + lenNow '截掉多余部分 if lenWord <= (lenMaxWord - Len(strTail)) then RetString = RetString + strWord else RetString = RetString + strTail lenWord = lenWord + Len(strTail) - lenNow if (lenMaxWord-lenWord)>0 then for j =1 to lenMaxWord-lenWord strSpaceBar = strSpaceBar + " " next end if GetTrueLength = RetString exit for end if next else '字符不溢出,填充空位 RetString = strChinese if (lenMaxWord-lenTotal)>0 then for i =1 to lenMaxWord-lenTotal strSpaceBar = strSpaceBar + " " next end if GetTrueLength = RetString ''''''''''& strSpaceBar end if end function %>
文章来源:www.sensheng.net.cn |
文章点评 |
|
|
 |
|
|