ASP中一个字符串处理类(VBScript)

这个类是用于处理字符串的,是老外写的,我把里面的功能和参数加了说明

使用方法:

=============== test.asp================

<!--#include file="StringOperations.asp"-->

<%
dim str
set str = New StringOperations
 test = str.toCharArray("check this out")
 response.write "<strong>str.toCharArray</strong>: "
 for i = 0 to ubound(test)
  response.write test(i) & " "
 next
 
 response.write "<BR><BR>"
 test1 = str.arrayToString(test)
 response.write "<strong>str.arrayToString</strong>: " & test1
 
 response.write "<BR><BR>"
 response.write "<strong>str.startsWith</strong>: " & str.startsWith(test1, "ch")
 
 response.write "<BR><BR>"
 response.write "<strong>str.endWith</strong>: " & str.endsWith(test1, "out")
 
 response.write "<BR><BR>"
 response.write "<strong>str.clone</strong>: " & str.clone("abc", 10)
 
 response.write "<BR><BR>"
 response.write "<strong>str.trimStart</strong>: " & str.trimStart(test1, 3)
 
 response.write "<BR><BR>"
 response.write "<strong>str.trimEnd</strong>: " & str.trimEnd(test1, 2)
 
 response.write "<BR><BR>"
 response.write "<strong>str.swapCase</strong>: " & str.swapCase("HiHiHi")
 
 response.write "<BR><BR>"
 response.write "<strong>str.isAlphabetic</strong>: " & str.isAlphabetic("!")
 
 response.write "<BR><BR>"
 response.write "<strong>str.capitalize</strong>: " & str.capitalize("clara fehler")
Set str = Nothing
%>

=============== StringOperations.asp================


<%
class StringOperations

 '****************************************************************************
 '' @功能说明: 把字符串换为char型数组
 '' @参数说明:  - str [string]: 需要转换的字符串
 '' @返回值:   - [Array] Char型数组
 '****************************************************************************
 public function toCharArray(byVal str)
  redim charArray(len(str))
  for i = 1 to len(str)
   charArray(i-1) = Mid(str,i,1)
  next
  toCharArray = charArray
 end function
 
 '****************************************************************************
 '' @功能说明: 把一个数组转换成一个字符串
 '' @参数说明:  - arr [Array]: 需要转换的数据
 '' @返回值:   - [string] 字符串
 '****************************************************************************
 public function arrayToString(byVal arr)
  for i = 0 to UBound(arr)
   strObj = strObj & arr(i)
  next
  arrayToString = strObj
 end function
 
 '****************************************************************************
 '' @功能说明: 检查源字符串str是否以chars开头
 '' @参数说明:  - str [string]: 源字符串
 '' @参数说明:  - chars [string]: 比较的字符/字符串
 '' @返回值:   - [bool]
 '****************************************************************************
 public function startsWith(byVal str, chars)
  if Left(str,len(chars)) = chars then
   startsWith = true
  else
   startsWith = false
  end if
 end function
 
 '****************************************************************************
 '' @功能说明: 检查源字符串str是否以chars结尾
 '' @参数说明:  - str [string]: 源字符串
 '' @参数说明:  - chars [string]: 比较的字符/字符串
 '' @返回值:   - [bool]
 '****************************************************************************
 public function endsWith(byVal str, chars)
  if Right(str,len(chars)) = chars then
   endsWith = true
  else
   endsWith = false
  end if
 end function
 
 '****************************************************************************
 '' @功能说明: 复制N个字符串str
 '' @参数说明:  - str [string]: 源字符串
 '' @参数说明:  - n [int]: 复制次数
 '' @返回值:   - [string] 复制后的字符串
 '****************************************************************************
 public function clone(byVal str, n)
  for i = 1 to n
   value = value & str
  next
  clone = value
 end function
 
 '****************************************************************************
 '' @功能说明: 删除源字符串str的前N个字符
 '' @参数说明:  - str [string]: 源字符串
 '' @参数说明:  - n [int]: 删除的字符个数
 '' @返回值:   - [string] 删除后的字符串
 '****************************************************************************
 public function trimStart(byVal str, n)
  value = Mid(str, n+1)
  trimStart = value
 end function
 
 '****************************************************************************
 '' @功能说明: 删除源字符串str的最后N个字符串
 '' @参数说明:  - str [string]: 源字符串
 '' @参数说明:  - n [int]: 删除的字符个数
 '' @返回值:   - [string] 删除后的字符串
 '****************************************************************************
 public function trimEnd(byVal str, n)
  value = Left(str, len(str)-n)
  trimEnd = value
 end function
 
 '****************************************************************************
 '' @功能说明: 检查字符character是否是英文字符 A-Z or a-z
 '' @参数说明:  - character [char]: 检查的字符
 '' @返回值:   - [bool] 如果是英文字符,返回TRUE,反之为FALSE
 '****************************************************************************
 public function isAlphabetic(byVal character)
  asciiValue = cint(asc(character))
  if (65 <= asciiValue and asciiValue <= 90) or (97 <= asciiValue and asciiValue <= 122) then
   isAlphabetic = true
  else
   isAlphabetic = false
  end if
 end function
 
 '****************************************************************************
 '' @功能说明: 对str字符串进行大小写转换
 '' @参数说明:  - str [string]: 源字符串
 '' @返回值:   - [string] 转换后的字符串
 '****************************************************************************
 public function swapCase(str)
  for i = 1 to len(str)
   current = mid(str, i, 1)
   if isAlphabetic(current) then
    high = asc(ucase(current))
    low = asc(lcase(current))
    sum = high + low
    return = return & chr(sum-asc(current))
   else
    return = return & current
   end if
  next
  swapCase = return
 end function
 
 '****************************************************************************
 '' @功能说明: 将源字符串str中每个单词的第一个字母转换成大写
 '' @参数说明:  - str [string]: 源字符串
 '' @返回值:   - [string] 转换后的字符串
 '****************************************************************************
 public function capitalize(str)
  words = split(str," ")
  for i = 0 to ubound(words)
   if not i = 0 then
    tmp = " "
   end if
   tmp = tmp & ucase(left(words(i), 1)) & right(words(i), len(words(i))-1)
   words(i) = tmp
  next
  capitalize = arrayToString(words)
 end function

end class
%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
函数 语法 功能 Len Len(string|varname) 返回字符串内字符的数目,或是存储一变量所需的字节数。 Trim Trim(string) 将字符串前后的空格去掉 Ltrim Ltrim(string) 将字符串前面的空格去掉 Rtrim Rtrim(string) 将字符串后面的空格去掉 Mid Mid(string,start,length) 从string字符串的start字符开始取得length长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串 Left Left(string,length) 从string字符串的左边取得length长度的字符串 Right Right(string,length) 从string字符串的右边取得length长度的字符串 LCase LCase(string) 将string字符串里的所有大写字母转化为小写字母 UCase UCase(string) 将string字符串里的所有大写字母转化为大写字母 StrComp StrComp(string1,string2[,compare]) 返回string1字符串string2字符串的比较结果,如果两个字符串相同,则返回0,如果小于则返回-1,如果大于则返回1 InStr InStr(string1,string2[,compare]) 返回string1字符串string2字符串第一次出现的位置 Split Split(string1,delimiter[,count[,start]]) 将字符串根据delimiter拆分成一维数组,其delimiter用于标识子字符串界限。如果省略,使用空格("")作为分隔符。count返回的子字符串数目,-1指示返回所有子字符串。start为1执行文本比较;如果为0或者省略执行二进制比较。 Replace Replace(expression,find,replacewith[,compare[,count[,start]]]) 返回字符串,其指定数目的某子字符串(find)被替换为另一个字符串(replacewith)。
以下是一个简单的 ASP 程序,用于在点击 Button1 按钮时向数据库添加记录。假设数据库连接字符串为 "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;",数据表名为 "myTable",需要添加的字段为 "Name" 和 "Age": ``` <%@ Language=VBScript %> <% Dim conn, cmd, name, age name = Request.Form("name") ' 获取表单提交的 Name 值 age = Request.Form("age") ' 获取表单提交的 Age 值 Set conn = Server.CreateObject("ADODB.Connection") ' 创建连接对象 conn.Open "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" ' 打开数据库连接 Set cmd = Server.CreateObject("ADODB.Command") ' 创建命令对象 cmd.ActiveConnection = conn ' 指定命令对象连接的数据库连接 cmd.CommandType = adCmdText ' 指定命令对象的型为文本 cmd.CommandText = "INSERT INTO myTable (Name, Age) VALUES (?, ?)" ' 指定插入语句 cmd.Parameters.Append cmd.CreateParameter("@Name", adVarChar, adParamInput, 50, name) ' 指定 Name 参数 cmd.Parameters.Append cmd.CreateParameter("@Age", adInteger, adParamInput, , age) ' 指定 Age 参数 cmd.Execute ' 执行命令 conn.Close ' 关闭数据库连接 Set cmd = Nothing ' 释放命令对象 Set conn = Nothing ' 释放连接对象 %> <html> <head> <title>Add Record</title> </head> <body> <form method="post" action=""> Name: <input type="text" name="name"><br> Age: <input type="text" name="age"><br> <input type="submit" name="Button1" value="Add Record"> </form> </body> </html> ``` 在上面的代码,我们首先通过 `Request.Form` 获取表单提交的 Name 和 Age 值,然后创建连接对象,打开数据库连接。接着创建命令对象,指定插入语句和参数,执行命令,最后释放对象并返回表单页面。需要注意的是,上面代码的 `adCmdText` 和 `adParamInput` 是常量,需要在代码声明或者引用相应的型库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值