几个好用的Asp自定义函数

来源:51windows.net 时间:2007-09-26 14:28:00 

GetRepeatTimes(TheChar,TheString) 得到一个字符串在另一个字符串当中出现几次的函数(新)

如:

response.write GetRepeatTimes("w",www.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 = "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


推荐阅读:

[精品]ASP中常用的22个FSO文件操作函数 

8个asp生成随机字符的函数

UTF-8转为GB2312编码的asp函数

标签:函数
0
投稿

猜你喜欢

  • MySql中流程控制函数/统计函数/分组查询用法解析

    2024-01-26 09:33:38
  • 基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)

    2022-12-20 08:44:05
  • 手机使用python操作图片文件(pydroid3)过程详解

    2021-06-19 13:45:39
  • 简单介绍Python的Django框架的dj-scaffold项目

    2021-11-01 07:18:05
  • 基于Python3.7.1无法导入Numpy的解决方式

    2022-10-27 20:50:01
  • python异常处理try的实例小结

    2022-01-25 06:06:51
  • python str字符串转uuid实例

    2021-12-31 20:15:54
  • Python+Pytorch实战之彩色图片识别

    2022-09-12 13:57:39
  • python数据爬下来保存的位置

    2021-01-29 20:49:19
  • CSS制作圆角导航的另一思路

    2008-11-06 11:39:00
  • 详解python中__name__的意义以及作用

    2021-11-20 19:44:24
  • python简单程序读取串口信息的方法

    2024-01-02 02:42:18
  • 基于Go和PHP语言实现爬楼梯算法的思路详解

    2024-05-22 10:18:20
  • sql server update 表的问题

    2009-10-04 20:32:00
  • 如何用OleDbDataAdapter来对数据库进行操作?

    2010-06-12 12:56:00
  • Python开发的实用计算器完整实例

    2023-07-25 17:11:53
  • sqlserver2005 行列转换实现方法

    2024-01-14 01:34:56
  • 离线安装Pyecharts的步骤以及依赖包流程

    2021-12-16 11:43:12
  • 教你使用pyqt实现桌面歌词功能

    2021-04-22 16:11:04
  • VUE+Element实现增删改查的示例源码

    2024-05-09 09:32:47
  • asp之家 网络编程 m.aspxhome.com