|
|
Option Public Option Explicit Option Compare Nocase
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' StringEx类,增强字符串对象 ' 现有功能:字符串拆分数组;Escape;UnEscape ' ' ----------By FangZeYu 2003-11-05 ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Class StringEx Private mValue As String Private vEsps As Variant '仅仅是一个全局变量,不能算数据存储内容
Public Sub New(strValue As String) mValue=strValue End Sub
Public Property Get Value() As String Value=mValue End Property Public Property Set Value() As String mValue=Value End Property
Public Property Get Escape() As String ' Const cstEsps="0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,44,58,59,60,61,62,63,91,92,93,94,96,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255" Const cstEsps="0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,44,58,59,60,61,62,63,91,92,93,94,96" If Isempty(vEsps) Then '保证vEsps数组只需要求值一次 Dim strT As New StringEx(cstEsps) vEsps=strT.Split(",",True) End If Dim str1,int1,strRet As String Dim i As Integer For i=1 To Len(mValue) str1=Mid(mValue,i,1) int1=Uni(str1) If int1>255 Then strRet=strRet+"%u"+Right("0000"+Hex$(int1),4) Elseif int1>122 Then strRet=strRet+"%"+Right("00"+Hex$(int1),2) Elseif Not Isnull(Arraygetindex(vEsps,Cstr(int1))) Then strRet=strRet+"%"+Right("00"+Hex$(int1),2) Else 'normal chars strRet=strRet+str1 End If Next Escape=strRet End Property
Public Property Get UnEscape() As String On Error Goto error_handle
Dim pos1,pos2 Dim strT As String,str1 Dim strRet As String strT=mValue While strT<>"" pos1=Instr(strT,"%") If pos1>0 Then strRet=strRet+Left(strT,pos1-1) '将%前的原义字符加入 '处理转义字符 pos2=Instr(strT,"%u") '%u If pos2=pos1 Then '如果是一个255以上的编码 str1=Mid(strT,pos2+2,4) If Len(str1)<4 Then '不合法的字符串 Goto error_handle End If strRet=strRet+Uchr(Clng("&H"+str1)) strT=Right(strT,Len(strT)-pos2-5) Else str1=Mid(strT,pos1+1,2) If Len(str1)<2 Then '不合法的字符串 Goto error_handle End If strRet=strRet+Uchr(Clng("&H"+str1)) strT=Right(strT,Len(strT)-pos1-2) End If Else strRet=strRet+strT strT="" End If Wend UnEscape=strRet
Exit Property error_handle: UnEscape="" Exit Property End Property
Public Property Get Split(strSep As String,bolCanEmpty As Integer) As Variant Const cstMaxTop=100 Redim vRet(cstMaxTop) As String Dim intCount As Integer Dim strT As String,str1 As String strT=mValue+strSep If strSep="" Then '如果分隔符设置为"",则将字符串直接分解 Dim i Redim vRet(Len(strT)) For i=1 To Len(strT) vRet(i)=Mid(strT,i,1) Next Split=vRet Exit Property End If While strT<>"" str1=Strleft(strT,strSep) strT=Strright(strT,strSep) If bolCanEmpty Or str1<>"" Then vRet(intCount)=str1 intCount=intCount+1 If intCount Mod cstMaxTop=0 Then Redim Preserve vRet(intCount+cstMaxTop) End If End If Wend Redim Preserve vRet(intCount-1) Split=vRet End Property
End Class
|
|