永发信息网

ASP一个关于取字符串长度的问题

答案:3  悬赏:70  手机版
解决时间 2021-04-08 01:38
  • 提问者网友:城市野鹿
  • 2021-04-07 01:31
本来我是用cutstr取固定长度的,今天不知怎么了,老出错.我是这样写的
<%=cutstr(rs("content"),20)
提示:类型不匹配: 'cutstr'

不知道该怎么解决?
最佳答案
  • 五星知识达人网友:woshuo
  • 2021-04-07 02:54
我一般用
<%
if len(rs("content"))>20 then
response.write(left(rs("content"),19)&"...")
else
response.write(rs("content"))
end if
%>
来代替CutStr函数,从来不出错,呵

CutStr函数参数说明:str,待截取的原字符串;gtype,截取方向,1为左,0为右;lennum为截取长度
当str的长度不超过lennum时,不执行截取,如超过,则截取,并在输出的字符串尾加上"..."

Public Function CutStr(Str, Gtype, LenNum)
Dim P_Num
Dim i, X
If StrLen(Str) <= LenNum Then
CutStr = 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
If Gtype = 1 Then
CutStr = Left(Trim(Str), X) & "..."
Else
CutStr = "..." & Right(Trim(Str), X)
End If
Loop
End If
End Function

’取字符串长度
Public Function StrLen(Str)
Dim P_Len, XX
P_Len = 0
StrLen = 0
If Not IsNull(Str) And Trim(Str) <> "" Then
P_Len = Len(Trim(Str))
For XX = 1 To P_Len
If Asc(Mid(Str, XX, 1)) < 0 Then
StrLen = CLng(StrLen) + 2
Else
StrLen = CLng(StrLen) + 1
End If
Next
End If
End Function
全部回答
  • 1楼网友:雾月
  • 2021-04-07 03:30
我没用过cutstr 从左边截取字符串可以用left(字符串,截取长度) 从由边截取字符串可以用right(字符串,截取长度) 从中间截取字符串可以用mid(字符串,起始位置,截取长度)
  • 2楼网友:走死在岁月里
  • 2021-04-07 03:12
你的问题有毛病:如果一个中文=2个英文的话,30跟16的关系就。。。 用我这个函数 function lefttitle(str,strlen,other) dim l,t,c, i,lablestr,regex,match,matches if strlen=0 then lefttitle="" exit function end if if isnull(str) then lefttitle = "" exit function end if if str = "" then lefttitle="" exit function end if str=replace(replace(replace(replace(str,"&nbsp;"," "),"&quot;",chr(34)),"&gt;",">"),"&lt;","<") l=len(str) t=0 strlen=clng(strlen) 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 lefttitle=left(str,i)&other exit for else lefttitle=str end if next lefttitle = replace(replace(replace(replace(lefttitle," ","&nbsp;"),chr(34),"&quot;"),">","&gt;"),"<","&lt;") end function
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯