<% ' +---------------------------------------------------------------------- ' | POPASP [ ASP MVC ] ' +---------------------------------------------------------------------- ' | Copyright (c) 2016 http://popasp.com All rights reserved. ' +---------------------------------------------------------------------- ' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) ' +---------------------------------------------------------------------- ' | Author: popasp <1737025626@qq.com> ' +---------------------------------------------------------------------- Class POPASP_STRING ''''''''''''''修改函数 '采用递归,删除str左边出现的字符串find function [ltrim](str,find) if left( str, len(find) ) = find Then [ltrim] = Me.[ltrim]( mid(str,len(find)+1) , find ) else [ltrim] = str End If End Function '采用递归,删除str右边出现的字符串find function [rtrim](str,find) if right( str, len(find) ) = find Then [rtrim] = Me.[rtrim]( left(str,len(str) - len(find) ) , find ) else [rtrim] = str End If End Function '采用递归,删除str两端出现的字符串find function [trim](str,find) [trim] = Me.rtrim(Me.ltrim(str,find),find) End Function '使用另一个字符串填充字符串为指定长度,两侧填充 'input: 输入字符串。 'pad_length: 如果 pad_length 的值是负数,小于或者等于输入字符串的长度,不会发生任何填充。 'pad_string: 如果填充字符的长度不能被 pad_string 整除,那么 pad_string 可能会被缩短。 function pad(ByVal input,ByRef pad_length,ByVal pad_string) pad = padding( input,pad_length,pad_string,"both" ) end Function '使用另一个字符串填充字符串为指定长度,右填充 '参数同pad function rpad(ByVal input,ByRef pad_length,ByVal pad_string) rpad = padding( input,pad_length,pad_string,"right" ) end Function '使用另一个字符串填充字符串为指定长度,左填充 '参数同pad function lpad(ByVal input,ByRef pad_length,ByVal pad_string) lpad = padding( input,pad_length,pad_string,"left" ) end Function '返回字符串,此字符串与指定字符串顺序相反。 Function Reverse( ByRef str ) reverse = StrReverse(str) End Function ' 重复一个字符串 ' input: 待操作的字符串 ' multiplier : input 被重复的次数 ' multiplier 必须大于等于 0。如果 multiplier 被设置为 0,函数返回空字符串 Function Repeat( ByRef input,ByRef multiplier ) dim i if multiplier = 0 then Repeat = "" : Exit Function elseif multiplier < 0 then Repeat = input : Exit Function end if for i = 1 to multiplier Repeat = Repeat & input next End Function 'server.urlencode的改写 Function URLEncode(ByVal input) URLEncode = Server.URLEncode(input) End Function '对server.urlencode函数的解码 function URLDecode(ByVal input) URLDecode = "" Dim sl: sl = 1 Dim tl: tl = 1 Dim key: key = "%" Dim kl: kl = Len(key) sl = InStr(sl, input, key, 1) Do While sl>0 If (tl=1 And sl<>1) Or tl 0) End Function Function iExists( input,find ) iExists = (inStr( LCase(input),LCase(find) ) > 0) End Function '判断某字符串是否以另一字符串开头 Function StartsWith( ByVal input,ByVal find ) StartsWith = (cmp( left(input,len( find )) , find ) = 0) End Function '判断某字符串是否以另一字符串开头,忽略大小写 Function iStartsWith( ByVal input,ByVal find ) iStartsWith = (casecmp( left(input,len( find )) , find ) = 0) End Function '判断某字符串是否以另一字符串结尾 Function EndsWith( ByVal input,ByVal find ) EndsWith = (cmp( right(input,len( find )) , find ) = 0) End Function '判断某字符串是否以另一字符串结尾,忽略大小写 Function iEndsWith( ByVal input,ByVal find ) iEndsWith = (casecmp( right(input,len( find )) , find ) = 0) End Function '''''''''''''''比较函数 '判断两字符串是否相同 Function Equal( ByVal str1,ByVal str2 ) Equal = (cmp( str1,str2 ) = 0) End Function '判断两字符串是否相同,忽略大小写 Function iEqual( ByVal str1,ByVal str2 ) iEqual = (casecmp( str1,str2 ) = 0) End Function '将字符串的首字母转换为大写 Function UCFirst(ByRef str) Dim first if str="" Then UCFirst = "" Else first = Mid(str,1,1) UCFirst = UCase(first) & Mid(str,2) End If End Function '将字符串的首字母转换为小写 Function LCFirst(ByRef str) Dim first if str="" Then LCFirst = "" Else first = Mid(str,1,1) LCFirst = LCase(first) & Mid(str,2) End If End Function ' 二进制安全比较字符串大小 function cmp( ByRef str1,ByRef str2 ) cmp = strComp( str1, str2 ) End Function ' 二进制安全比较字符串大小,忽略大小写 function casecmp( ByRef str1,ByRef str2 ) casecmp = cmp( LCase(str1), LCase(str2) ) End Function '查找字符串的首次出现,返回字符串的一部分或者 Empty(如果未发现) Function str( ByRef input ,ByRef find ) Dim pos pos = inStr(input,find) if pos>0 then str = mid( input,pos ) End Function '查找字符串的最后一次出现,返回字符串的一部分或者 Empty(如果未发现) Function rstr( ByRef input ,ByRef find ) Dim pos pos = inStrRev( input,find ) if pos>0 then rstr = mid( input,pos ) End Function '使用另一个字符串填充字符串为指定长度 'input: 输入字符串。 'pad_length: 如果 pad_length 的值是负数,小于或者等于输入字符串的长度,不会发生任何填充。 'pad_string: 如果填充字符的长度不能被 pad_string 整除,那么 pad_string 可能会被缩短。 'action: 填充方向,可为left、right、both Private function padding(ByVal input,ByRef pad_length,ByVal pad_string,ByRef action) dim input_length,padding_length input_length = len(input) if pad_length <= input_length Then padding = input : exit function end if pad_string = CStr(pad_string) if pad_string = "" then pad_string = " " padding_length = len( pad_string ) '循环填充 do until input_length >= pad_length if action = "left" then input = pad_string & input else input = input & pad_string end if input_length = input_length + padding_length '两侧填充时,得实时检测其长度 if action = "both" and input_length < pad_length then input = pad_string & input input_length = input_length + padding_length end if loop '填充长了,还得修剪 if input_length > pad_length then if action = "left" then input = right( input,pad_length ) else input = left( input,pad_length ) end if end if padding = input end Function '对正则匹配模式进行设置 Private Sub setReg(ByRef pattern, ByRef mode) '忽略大小写 If iExists(mode,"i") Then POP_MVC.reg.IgnoreCase = true Else POP_MVC.reg.IgnoreCase = false End If '全部 If iExists(mode,"g") Then POP_MVC.reg.Global = true Else POP_MVC.reg.Global = false End If '多行 If iExists(mode,"m") Then POP_MVC.reg.MultiLine = true Else POP_MVC.reg.MultiLine = false End If POP_MVC.reg.Pattern = pattern End Sub '将HTML文本转换为HTML代码 Public Function HtmlDecode(ByVal str) If len(str) > 0 Then str = Replace(str, "&" , Chr(38)) str = Replace(str, """", Chr(34)) str = Replace(str, "<" , Chr(60)) str = Replace(str, ">" , Chr(62)) str = Replace(str, " " , Chr(32)) End If HtmlDecode = str End Function '''''''''''''''排序函数 '将字符串中的字符按升序排列 Function sort(ByRef str) dim arr arr = explode(str) call POP_MVC.Array.sort( arr ) sort = join(arr,"") End Function '将字符串中的字符按降序排列 Function rsort(ByRef str) dim arr arr = explode(str) call POP_MVC.Array.rsort( arr ) rsort = join(arr,"") End Function '按自定义函数将字符串中的字符排列 Function usort( ByRef str , ByRef FuncComp) dim arr arr = explode(str) call POP_MVC.Array.usort( arr , funcComp) usort = join(arr,"") End Function ''''''''''''''其他函数 '将字符串按每N(默认为1)个字符炸成一个数组 Function explode( ByRef mixed ) dim str,separator,iBound,i,length,arr separator = 1 if isArray( mixed ) then '如果为数组 ibound = ubound( mixed ) if ibound < 0 then '数组中无元素,则返回该空数组 explode = mixed exit Function end if str = mixed(0) if ibound > 0 then '默认第二个元素为步长 separator = mixed(1) end if else '默认为字符串,如果传递其他类型的值可能会出错 str = mixed end if length = len(str) arr = Array() if isNumeric( separator ) then for i = 1 to length step separator POP_MVC.Array.push arr,mid(str,i,separator) next elseif typename( separator ) = "String" then arr = split( str,separator ) end if explode = arr End Function '计算字符串的 MD5 散列值,使用了Easp的插件 Function md5(ByRef str) dim startTime if not POP_MVC.dMd5.exists( str ) then startTime = timer() md5 = P_("POPASP_MD5").To32( str ) POP_MVC.dMd5(str) = md5 call POP_MVC.pushTime( startTime , "MD5 " & md5 & " ( length : " & byte2size(len(str)) & ")" ) else md5 = POP_MVC.dMd5(str) end if End Function ' 产生随机字串 ' @param string randLen 长度 ' @param string randType 字串类型 ' 0 字母 1 数字 2 大写字母 3 小写字母 4 汉字 其它 大小写字母与数字混合 ' @return string Function Random( ByRef randLen,ByRef randType ) dim chars_len,chars,i if randLen < 1 then Random = "" Exit Function end if Select case randType case 0 '字母 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" case 1 '数字 chars = Repeat( "0123456789" , 3 ) case 2 '大写字母 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" case 3 ' 小写字母 chars = "abcdefghijklmnopqrstuvwxyz" case 4 chars = POP_MVC.file_get_contents( POP_MVC.mvc_dir & "Tpl/commonChineseCharacter.txt" ) case else chars = "ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789" End Select if randLen > 10 then '位数过长重复字符串一定次数 if randType = 1 then chars = Repeat( chars,randLen ) else chars = Repeat( chars, 5 ) end if end if if randType <> 4 then chars = Shuffle( chars ) Random = mid( chars,1,randLen ) else Random = "" chars_len = len( chars ) + 1 for i = 1 to randLen Random = Random & mid( chars, int(Rnd() * chars_len),1 ) next end if End Function '移除字符串中重复的字符 Function unique(ByRef str) dim arr arr = explode(str) arr = POP_MVC.Array.unique( arr ) unique = join(arr,"") End Function '将字符串中的字符打乱 Function Shuffle( ByRef str ) dim arr arr = explode(str) call POP_MVC.Array.Shuffle( arr ) Shuffle = join(arr,"") End Function '二进制安全比较字符串开头的若干个字符 function ncmp( ByRef str1,ByRef str2,ByRef length) ncmp = cmp( left(str1,length) , left(str2,length) ) end Function '二进制安全比较字符串开头的若干个字符,忽略大小写 function ncasecmp( ByRef str1,ByRef str2, ByRef length) ncasecmp = ncmp( LCase(str1) , LCase(str2),length ) end Function '使用自然排序算法比较字符串,忽略大小写 function casenatcmp(ByVal left,ByVal right ) casenatcmp = natcmp(LCase(left),LCase(right) ) End Function '使用自然排序算法比较字符串 function natcmp(ByVal left,ByVal right) dim reg dim lTest,rTest,matches,test set reg = POP_MVC.reg reg.Global = false : reg.IgnoreCase = true : reg.MultiLine = false while (len(left) > 0) and (len(right) > 0) reg.Pattern = "^([^0-9]*)([0-9].*)$" '第一步,以数字为分界,将字符串分成两部分,先比较第一部分 if reg.test(left) then set matches = reg.execute( left ) lTest = matches(0).subMatches(0) left = matches(0).subMatches(1) else lTest = left left = "" end if if reg.test(right) then set matches = reg.execute( right ) rTest = matches(0).subMatches(0) right = matches(0).subMatches(1) else rTest = right right = "" end if '进行字符串比较,如果值不为0,则返回该值 test = cmp( lTest , rTest ) If test<>0 Then natcmp = test Exit Function End If '第二步,将分出来的第二部分再用数字进行分割,并比较数字部分 reg.Pattern = "^([0-9]+)([^0-9].*)?$" if reg.test(left) Then set matches = reg.execute(left) lTest = CLng( matches(0).subMatches(0) ) left = matches(0).subMatches(1) else lTest = 0 End if if reg.test(right) then set matches = reg.execute(right) rTest = CLng( matches(0).subMatches(0) ) right = matches(0).subMatches(1) else rTest = 0 end if '进行数字比较,如果值不为0,则返回该值 test = lTest - rTest if test<> 0 Then natcmp = test exit Function end if Wend natcmp = cmp( left,right ) set matches = nothing End Function '获取A:B中的A Function Before( byref str,byref separator ) dim pos pos = inStr( str , separator ) if pos then Before = mid(str,1,pos-1) else Before = str end if End Function '获取A:B中的B Function After( byref str,byref separator ) dim pos pos = inStr( str , separator ) if pos then After = mid(str,pos + len(separator) ) else After = str end if End Function '返回字符串的长度,中文算两个字符 Private Function cnlen(str) Dim i,n : n = 0 For i = 1 To Len(str) if Abs(Ascw(Mid(str,i,1)))>255 then n = n + 2 else n = n + 1 end if Next cnlen = n End Function '过滤HTML标签 Public Function HtmlFilter(ByVal str) if str <> "" then str = Me.reg_replace(str,"","<[^>]+>","gm") str = Replace(str, ">", ">") str = Replace(str, "<", "<") End If HtmlFilter = str End Function '截取长字符串的指定长度并以...代替 '半角字符以半个字符计,返回的字符串最大长度为strlen '因为字体不同,该函数并不能保证所截出的字符串长短一致,最好还是使用CSS进行截取 Public Function Cut(ByVal s, ByVal strlen) if s = "" then Cut = "" : Exit Function '去除html标签、换行和制表符 s = Me.HtmlFilter(s) s = replace(s, vbCrLf,"") s = replace(s, vbTab, "") if is_empty( strlen ) then Cut = s : Exit Function Dim suffix , i, n suffix = "..." '解决中英文混排长度 n = 0 Cut = s For i = 1 To Len(s) if Abs(Ascw(Mid(s,i,1)))>255 then n = n + 2 If n >= strlen-1 Then Cut = Left(s, i) & suffix Exit For End If else n = n + 1 If n >= strlen Then Cut = Left(s, i) & suffix Exit For End If end if Next End Function '将Byte()类型转化为字符串 Function byte2str(bytes, charset) on error resume next dim oStrm set oStrm = POP_MVC.CreateStream With oStrm .Type = 1 .Mode =3 .Open .Write bytes .Position = 0 .Type = 2 .Charset = charset byte2str = .ReadText .Close End With set oStrm = nothing Call L_("POPASP_STRING.byte2str") End Function '替换正则表达式编组 '说明:按正则表达式的规则替换一个字符串中某个捕获编组的内容 '示例:reg_replace2("photo-3.html", "^(\w+)-(\d+)\.html$", "$2", "4") ' 返回: photo-4.html Public Function reg_replace2(ByVal str, ByVal rule, ByVal group, ByVal replaceWith) If Not Me.reg_test(str, rule,"gim") Then '如果规则不匹配则直接返回字符串 reg_replace2 = str Exit Function End If Dim o_match, i, j, s_match, i_pos, s_left, s_tmp '获取编组号 i = Int(Mid(group,2))-1 '取得正则编组 Set o_match = Me.reg_exec(str,rule,"gim")(0) '循环编组查找匹配项 For j = 0 To o_match.SubMatches.Count-1 s_match = o_match.SubMatches(j) '取得当前组的字符开始位置 i_pos = Instr(str,s_match) If i_pos > 0 Then '把字符串按当前组的位置分为两部分 s_tmp = Left(str,i_pos-1) str = Mid(str,Len(s_tmp)+1) '如果找到匹配的编组号则仅替换本组中的字符串 If i = j Then '把替换后的字符串和前一部分组合起来 reg_replace2 = s_left & s_tmp & Replace(str,s_match,replaceWith,i_pos-len(s_tmp),1,0) Exit For End If '如果没有找到匹配则把当前组的字符串换到前一部分中去 s_left = s_left & s_tmp & s_match '在后面部分的字符串中继续下一次扫描匹配 str = Mid(str, Len(s_match)+1) End If Next Set o_match = Nothing End Function End Class %>