几个常用的asp自造函数
|
admin
2010年7月23日 0:15
本文热度 5207
|
getrepeattimes(thechar,thestring) 得到一个字符串在另一个字符串当中出现几次的函数(新)
如:
response.write getrepeattimes("w",aspxhome.com)
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 = "123456"
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
该文章在 2010/7/23 0:15:32 编辑过