ASP的一个字符串处理类加强版
2006-02-05 13:55
387 查看
<%
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
varrayToString = 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
'****************************************************************************
'' @功能说明: 将源字符Str后中的'过滤为''
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
public function checkstr(Str)
If Trim(Str)="" Or IsNull(str) Then
checkstr=""
else
checkstr=Replace(Trim(Str),"'","''")
end if
End function
'****************************************************************************
'' @功能说明: 将字符串中的str中的HTML代码进行过滤
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
Public Function HtmlEncode(str)
If Trim(Str)="" Or IsNull(str) then
HtmlEncode=""
else
str=Replace(str,">",">")
str=Replace(str,"<","<")
str=Replace(str,Chr(32)," ")
str=Replace(str,Chr(9)," ")
str=Replace(str,Chr(34),""")
str=Replace(str,Chr(39),"'")
str=Replace(str,Chr(13),"")
str=Replace(str,Chr(10) & Chr(10), "</p><p>")
str=Replace(str,Chr(10),"<br> ")
HtmlEncode=str
end if
End Function
'****************************************************************************
'' @功能说明: 计算源字符串Str的长度(一个中文字符为2个字节长)
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [Int] 源字符串的长度
'****************************************************************************
Public Function strLen(Str)
If Trim(Str)="" Or IsNull(str) Then
strlen=0
else
Dim P_len,x
P_len=0
StrLen=0
P_len=Len(Trim(Str))
For x=1 To P_len
If Asc(Mid(Str,x,1))<0 Then
StrLen=Int(StrLen) + 2
Else
StrLen=Int(StrLen) + 1
End If
Next
end if
End Function
'****************************************************************************
'' @功能说明: 截取源字符串Str的前LenNum个字符(一个中文字符为2个字节长)
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - LenNum [int]: 截取的长度
'' @返回值: - [string]: 转换后的字符串
'****************************************************************************
Public Function CutStr(Str,LenNum)
Dim P_num
Dim I,X
If StrLen(Str)<=LenNum Then
Cutstr=Str
Else
P_num=0
X=0
Do While Not P_num > LenNum-2
X=X+1
If Asc(Mid(Str,X,1))<0 Then
P_num=Int(P_num) + 2
Else
P_num=Int(P_num) + 1
End If
Cutstr=Left(Trim(Str),X)&"..."
Loop
End If
End Function
end class
%>
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
varrayToString = 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
'****************************************************************************
'' @功能说明: 将源字符Str后中的'过滤为''
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
public function checkstr(Str)
If Trim(Str)="" Or IsNull(str) Then
checkstr=""
else
checkstr=Replace(Trim(Str),"'","''")
end if
End function
'****************************************************************************
'' @功能说明: 将字符串中的str中的HTML代码进行过滤
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
Public Function HtmlEncode(str)
If Trim(Str)="" Or IsNull(str) then
HtmlEncode=""
else
str=Replace(str,">",">")
str=Replace(str,"<","<")
str=Replace(str,Chr(32)," ")
str=Replace(str,Chr(9)," ")
str=Replace(str,Chr(34),""")
str=Replace(str,Chr(39),"'")
str=Replace(str,Chr(13),"")
str=Replace(str,Chr(10) & Chr(10), "</p><p>")
str=Replace(str,Chr(10),"<br> ")
HtmlEncode=str
end if
End Function
'****************************************************************************
'' @功能说明: 计算源字符串Str的长度(一个中文字符为2个字节长)
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [Int] 源字符串的长度
'****************************************************************************
Public Function strLen(Str)
If Trim(Str)="" Or IsNull(str) Then
strlen=0
else
Dim P_len,x
P_len=0
StrLen=0
P_len=Len(Trim(Str))
For x=1 To P_len
If Asc(Mid(Str,x,1))<0 Then
StrLen=Int(StrLen) + 2
Else
StrLen=Int(StrLen) + 1
End If
Next
end if
End Function
'****************************************************************************
'' @功能说明: 截取源字符串Str的前LenNum个字符(一个中文字符为2个字节长)
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - LenNum [int]: 截取的长度
'' @返回值: - [string]: 转换后的字符串
'****************************************************************************
Public Function CutStr(Str,LenNum)
Dim P_num
Dim I,X
If StrLen(Str)<=LenNum Then
Cutstr=Str
Else
P_num=0
X=0
Do While Not P_num > LenNum-2
X=X+1
If Asc(Mid(Str,X,1))<0 Then
P_num=Int(P_num) + 2
Else
P_num=Int(P_num) + 1
End If
Cutstr=Left(Trim(Str),X)&"..."
Loop
End If
End Function
end class
%>
相关文章推荐
- ASP中一个字符串处理类(加强)(VBScript)
- ASP中一个字符串处理类加强版
- ASP中一个字符串处理类加强版
- ASP中一个字符串处理类加强版
- ASP中一个字符串处理类加强版
- ASP中一个字符串处理类加强版
- ASP中一个字符串处理类(加强)(VBScript)
- ASP中一个字符串处理类(VBScript) 原版
- 关于asp.net 一个处理时间过长的处理办法
- mySQL字符串处理之一个字段包含多个ID的解决
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- Asp.net中如何处理一个站点不同Web应用通用Session的问题
- 假设有一个字符串aabcad,请写一段程序,去掉字符串中不相邻的重复字符串,即上述字符串处理之后的输出结果为:aabcd
- 改进 ASP 的字符串处理性能
- ASP.NET MVC Json()处理大数据异常解决方法,字符串的长度超过了为 maxJsonLength
- 改进 ASP 的字符串处理性能
- 哈希表对字符串的高效处理3:实现一个听高级的字符匹配算法
- iOS 一个在UDP/TCP通信时,对16进制、10进制、NSData等数据之间的转换发送等处理的工具类库,一行代码将16进制字符串轻松转换成UDP需要的指令数据
- ASP.NET Core改进了.NET Framework中的字符串处理
- python中如何处理一个字符串中的每一个字符