精美而实用的网站,关注web编程技术、网站运营、SEO推广,让您轻松愉快的学习

我们在工作的过程中经常会用到截取字符串函数,ASP自带的截取字符串函数有left、right等,但是自带的函数对汉字判断不是很准确,下面青岛星网跟大家分享一个自定义截取字符串函数。

ASP截取字符串函数可判断汉字

'text 内容
'length 需要的长度
'技术支持 www.qdxw.net
Function InterceptString(text,length) '函数名
 dim text_length
 dim count_length
 dim count
 dim ii
 text=trim(text) 
 text_length= len(text) '求字符串的长度
 count_length = 0 
 if text_length >= 1 then
 for count= 1 to text_length '这一个循环计算要截取的字符串
 if asc(mid(text,count,1)) < 0 or asc(mid(text,count,1)) >255 then '如果是汉字
 count_length = count_length + 2
 else
 count_length = count_length + 1
 end if
 if count_length >= length then
 text = left(trim(text),count)&"..." '字符串限长
 exit for
 end if
 next
 InterceptString = text '函数返回值
 else
 InterceptString = ""
 end if
End Function
Tags:ASP ASP函数 字符串