控制输出字符串长度(自动区别中英文)

商帝国咨询头像

阅读 9989 | 分类:文章

此ASP函数实现截取自定义长度的字符串,并可定义超出部分显示样式,支持中英文字符串自动判别


<%
'定义字符串判别函数
function strlen(str)
dim p_len
p_len=0
strlen=0
if trim(str)<>"" then
p_len=len(trim(str))
for xx=1 to p_len
if asc(mid(str,xx,1))<0 then
strlen=int(strlen) + 2
else
strlen=int(strlen) + 1
end if
next
end if
end function
'定义截取函数
function strvalue(str,lennum)
dim p_num
dim i
if strlen(str)<=lennum then
strvalue=str
else
p_num=0
x=0
do while not p_num > lennum-2
x=x+1
if asc(mid(str,x,1))<0 then
p_num=int(p_num) + 2
else
p_num=int(p_num) + 1
end if
strvalue=left(trim(str),x)&"…" //截取后定义超出部分内容的显示方式
loop
end if
end function
%>



使用方法:

1.首先请将以上蓝框内代码转存为short.asp

2.我们使用DWMX2004新建一个ASP页面,内容如下

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%'引入公共函数模块 %>
<!--#include file="short.asp"-->

3.对目标截取输出,例如对新闻标题字段内容进行截取

<% =strvalue(rsnews.Fields.Item("title").Value,20)
'20 为截取长度 %>

留言区(共0条)

暂无留言,快来抢占沙发~
作者声明:非原创
平台声明:文章及评论仅代表作者本人观点,五略商书系信息发布平台,仅提供信息存储空间服务,如有侵权,请联系13521509118。转载请注明作者和出处:转自五略商书m.shangdiguo.com,作者商帝国咨询!