首页
技术库|站长工具|技术手册|字体库|知识点词汇表| 联系我们|
打开本页的html静态页面
 

主菜单

文章分类

.: Asp技术 .: Asp代码库 .: asp中针对汉字求字符串的长度与截取字符串

  • 全文内容
  • 发表评论
  • 文章点评
  • 文章附件
  • Email文章
  • 打印文章

asp中针对汉字求字符串的长度与截取字符串

点击次数:316 创建日期:11-8-2007 录入:cn-web.com 字体:[ ] 点评:


因为汉字在字符串中算两个字符,所以在一般操作中需要另行处理:以下函数通用
 
'**************************************************
'函数名:strLength
'作  用:求字符串长度。汉字算两个字符,英文算一个字符。
'参  数:str  ----要求长度的字符串
'返回值:字符串长度
'**************************************************
function strLength(str)
 ON ERROR RESUME NEXT
 dim WINNT_CHINESE
 WINNT_CHINESE    = (len("中国")=2)
 if WINNT_CHINESE then
        dim l,t,c
        dim i
        l=len(str)
        t=l
        for i=1 to l
         c=asc(mid(str,i,1))
            if c<0 then c=c+65536
            if c>255 then
                t=t+1
            end if
        next
        strLength=t
    else
        strLength=len(str)
    end if
    if err.number<>0 then err.clear
end function

 

'函数名:gotTopic
'作  用:截字符串,汉字一个算两个字符,英文算一个字符
'参  数:str   ----原字符串
'   strlen ----截取长度
'返回值:截取后的字符串
'*************************************************
function gotTopic(str,strlen)
 if str="" then
  gotTopic=""
  exit function
 end if
 dim l,t,c, i
 str=replace(replace(replace(replace(str,"&nbsp;"," "),"&quot;",chr(34)),"&gt;",">"),"&lt;","<")
 l=len(str)
 t=0
 for i=1 to l
  c=Abs(Asc(Mid(str,i,1)))
  if c>255 then
   t=t+2
  else
   t=t+1
  end if
  if t>=strlen then
   gotTopic=left(str,i) & "..."
   exit for
  else
   gotTopic=str
  end if
 next
 gotTopic=replace(replace(replace(replace(gotTopic," ","&nbsp;"),chr(34),"&quot;"),">","&gt;"),"<","&lt;")
end function


请文明参与讨论,禁止漫骂攻击。
评论总数:0 [ 查看全部 ] 网友评论
此文章还没有任何评论!
(+5分)
(+4分)
(+3分)
(+2分)
(+1分)
此内容无附件
网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号