ndfweb.cn

Asp幾個自定義函數


2009-05-09 14:08:58 (5764)



GetRepeatTimes(TheChar,TheString) 得到一個字符串在另一個字符串當中出現幾次的函數()

如:
response.write GetRepeatTimes("w","www.51windows.net")
response.write GetRepeatTimes("ww","wwwww")

在網上看到過一個CheckTheChar(TheChar,TheString)函數,有個bug,在檢測wwwww中有幾個ww時,會錯誤的返回4個!

cLeft(string, length) 返回指定數目的從字符串的左邊算起的字符,區分單雙字節。

如:
Dim MyString, LeftString
MyString = "文字測試VBSCript"
LeftString = cLeft(MyString, 10)
返回 "文字測試VB"。


MyRandc(n) 生成隨機字符,n為字符的個數

如:
response.write MyRandn(10)
輸出10個隨機字符


MyRandn(n) 生成隨機數字,n為數字的個數

如:
response.write MyRandn(10)
輸出10個隨機數字


formatQueryStr(str) 格式化sql中的like字符串.
如:
q = Request("q")
q = formatQueryStr(q)
sql = "select * from [table] where aa like '%"& q &"%'"

GetRnd(min,max) 返回min - max之間的一個隨機數

如:
response.write GetRnd(100,200)
輸出大於100到200之間的一個隨機數

RegReplace(str,regexStr,RepalceStr) 對str  進行正則替換

如:
htmlstr = "123<img src=""asdf.gif"" border=""0"">45<b>6</b>"
htmlstr2 = RegReplace(htmlstr,"<(.[^><]*)>","")
返回 htmlstr2  為123456

所有函數如下:

function cLeft(str,n)
 dim str1,str2,alln,Islefted
 str2 = ""
 alln = 0
 str1 = str
 Islefted = false
 if isnull(str) then
  cleft = ""
  exit function
 end if
 for i = 1 to len(str1)
  nowstr = mid(str1,i,1)
  if asc(nowstr)<0 then
   alln = alln + 2
  else
   alln = alln + 1
  end if
  if (alln<=n) then
   str2 = str2 & nowstr
  else
   Islefted = true
   exit for
  end if
 next
 if Islefted then
  str2 = str2 & ".."
 end if
 cleft = str2
end function


function MyRandc(n) '生成隨機字符,n為字符的個數
 dim thechr
 thechr = ""
 for i=1 to n
  dim zNum,zNum2
  Randomize
  zNum = cint(25*Rnd)
  zNum2 = cint(10*Rnd)
  if zNum2 mod 2 = 0 then
   zNum = zNum + 97
  else
   zNum = zNum + 65
  end if
  thechr = thechr & chr(zNum)
 next
 MyRandc = thechr
end function


function MyRandn(n) '生成隨機數字,n為數字的個數
 dim thechr
 thechr = ""
 for i=1 to n
  dim zNum,zNum2
  Randomize
  zNum = cint(9*Rnd)
  zNum = zNum + 48
  thechr = thechr & chr(zNum)
 next
 MyRandn = thechr
end function

function formatQueryStr(str) '格式化sql中的like字符串
 dim nstr
 nstr = str
 nstr = replace(nstr,chr(0),"")
 nstr = replace(nstr,"'","''")
 nstr = replace(nstr,"[","[[]")
 nstr = replace(nstr,"%","[%]")
 formatQueryStr = nstr
end function

function GetRnd(min,max)
 Randomize
 GetRnd = Int((max - min + 1) * Rnd + min)
end function

Function GetRepeatTimes(TheChar,TheString)
 GetRepeatTimes = (len(TheString)-len(replace(TheString,TheChar,"")))/len(TheChar)
End Function

Function RegReplace(Str,PatternStr,RepStr)
 Dim NewStr,regEx
 NewStr = Str
 if isnull(NewStr) then
  RegReplace = ""
  exit function
 end if
 Set regEx = New RegExp
 regEx.IgnoreCase = True
 regEx.Global = True
 regEx.Pattern=PatternStr
 NewStr = regEx.Replace(NewStr,RepStr)
 RegReplace = NewStr
end function

本文版权:http://www.ndfweb.cn/news-338.html
  NDF俱乐部
  国际域名注册
  建站咨询
简体中文 NDF网站建设淘宝店 | ICO图标在线生成 | 外贸网站建设 | 联系我们
©2007-2025 NDF Corporation 鲁ICP备08005967号 Sitemap - RSSRSS订阅