asp 字串截取函數_應用技巧

來源:互聯網
上載者:User
asp 字串截取函數
'*********************************************************
'函數:cutStr[str(strlen)]
'參數:str,待處理的字串,strlen,截取的長度
'作者:木木
'日期:2007/7/12
'描述:截取指定長度的字串
'樣本:<%=cutStr("歡迎光臨阿里西西",5)%>

'*********************************************************

function cutStr(str,strlen)
 If str = "" Then
 cutStr = "cutStr函數異常:字串為空白"
 exit function
 End If
'------------來源長度檢查
 If  strlen = "" Then
 cutStr = "cutStr函數異常:長度未指定"
 exit function
 End If 

 If  CInt(strlen) = 0 Then
 cutStr = "cutStr函數異常:長度為0"
 exit function
 End If 
'----------檢測來源字元長度
 dim l,t,c,i
 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
 cutStr=left(str,i)&".."
 exit for
 else
 cutStr=str
 end if
 next
 cutStr=replace(cutStr,chr(10),"")
end function
''*********************************************************
'函數:strlen[str]
'參數:str,待處理的字串
'作者:木木
'日期:2007/7/12
'描述:判斷字串長度,漢字長度為2
'樣本:<%=strlen("歡迎光臨阿里西西")%>
'*********************************************************
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
截取左邊的n個字元'*********************************************************
'函數:LeftTrue(str,n)
'參數:str,待處理的字串,n,截取的長度
'作者:木木
'日期:2007/7/12
'描述:顯示左邊的n個字元(自動識別漢字)函數
'樣本:<%=LeftTrue("歡迎光臨阿里西西",6)%>
'*********************************************************

Function LeftTrue(str,n)
If len(str)<=n/2 Then
 LeftTrue=str
Else
 Dim TStr
 Dim l,t,c
 Dim i
 l=len(str)
 t=l
 TStr=""
 t=0
 for i=1 to l
  c=asc(mid(str,i,1))
  If c<0 then c=c+65536
  If c>255 then
  t=t+2
  Else
  t=t+1
  End If
  If t>n Then exit for
  TStr=TStr&(mid(str,i,1))
 next
 LeftTrue = TStr
End If
End Function


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.