ASP过长标题截断,月日截取

 方法一:
    在文章列表的时候,如果标题过长,往往会撑破表格破坏页面形象。一般做法是略去超长部分而以省略号代替。比如要取前10个字符,则可写出以下语句:
    
    if Len(title)>10 then title=Left(title,9)+"…"  
        '凑起来刚好10个字符
    
    而我们中国人要面对现实——汉字宽度是字母的两倍。所以得设计一个函数,用一个变量计算字符串长度,如果遇到的是字母,这个长度就加1,如果遇到的是汉字,长度加2:
    
    FUNCTION cuttitle(str,strlen)
        'str为待切标题,strlen为截取长度(按字母计算)
        dim tmplen,tmpstr,i,s
        tmpstr=""
        tmplen=0
        str=trim(str)
        if str="" then exit function
        for i=1 to Len(str)
            s=mid(str,i,1)
            tmpstr=tmpstr&s
            tmplen=tmplen+1
            if Asc(s)<0 then tmplen=tmplen+1
            '如果是汉字,长度再加1
            if tmplen>strlen then
                tmpstr=Left(tmpstr,Len(tmpstr)-2)&"…"
                exit for
            end if
        next
        cuttitle=tmpstr
    END FUNCTION
----------------------------------------------------------------------------
方法二:
<%
Function GetLen(Str)    '检测包含汉字字串的长度,一个汉字的长度为2,英文字母的长度为1
    Dim Strlen,i
    GetLen = 0
    Strlen=Len(Str)
    IF IsNull(Str) Then Exit Function End If
    For i = 1 To Strlen
        If Asc(Mid (Str,i,1)) < 0 Then
        GetLen = GetLen + 2
        Else
        GetLen = GetLen + 1
        End If
    Next
End Function

Function GetLeft(Str,L,Alter)     '获取字符串的长度并根据要求裁切,按汉字的长度计算,1个汉字的长度=2个英文字母的长度,其中str表示呆截取的字符串变量或常量,L表示截取后保留的长度,Alter表示截取后补的字符,一般为"....".
    Dim i,j
    j=1
    GetLeft = ""
    If GetLen(Str)>2*L Then
        For i=1 To L
            If Asc(Mid(Str,j,1)) < 0 or Asc(Mid(Str,j+1,2)) < 0 Then
            GetLeft=GetLeft & Mid(Str,j,1)
            j=j+1    
            Else
            GetLeft=GetLeft & Mid(Str,j,2)
            j=j+2
            End If
        Next
        If Alter<>"" Then
            GetLeft = GetLeft & Alter
        End If
    Else
        GetLeft = Str
    End If
End Function
%>
  其中Function GetLen(Str)函数是计算字符串长度的,我的计算规则是一个汉字的长度为2,英文字母的长度为1.
  Function GetLeft(Str,L,Alter) 函数是截断的函数.
  譬如要截断字符串"代码测试",使之只保留"代码",然后后补以"....",那么只需要
<%
  GetLeft("代码测试",2,"....")
%>
---------------------------------------------------------------------------------
方法三:(截取中英文字符串)
<%
function strLeft(str,num)
dim p_str,p_num
p_str = ""
p_num = 0    
if trim(str) <> "" then
     p_len = len(str)
     for i = 1 to p_len
             if asc(mid(str,i,1)) > 255 or asc(mid(str,i,1)) < 0 then
                 p_num = p_num + 2
             else
                 p_num = p_num + 1
             end if
        
             if p_num > num then
                 p_str = Left(str,i-1)  
                exit for
            else
                p_str = str
            end if
     next
end if
strLeft=p_str
end function
%>
______________________________________________


截取月日:
<%= Month(now()) %>-<%= day(now()) %>

function date_md(tim)
      dim m,d
      if Month(tim)>9 then
            m=Month(tim)
      else
            m="0"&Month(tim)
      end if
      if day(tim) >9 then
            d=day(tim)
      else
            d="0"&day(tim)
      end if
     date_md=m&"-"&d
end function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值