程序需要,做了一個自定義的檢測函數(shù),主要檢測字符串的類型,因為需求,將'和%也一同放入英文字符類型中。原理很簡單,將字符拆開單取獲得ASC碼,在什么范圍就為什么類型,當然檢測有局限性。
代碼如下: '############################################################# '名稱:StrType(Str) '用途:判斷字符串類型 '返回0-空 '返回1-數(shù)字 '返回2-英文 '返回3-漢字 '返回4-英漢 '返回5-英數(shù) '返回6-漢數(shù) '返回7-全 '############################################################# Function StrType(Str) On Error Resume Next Dim I,A,E,N,C E=False N=False C=False If IsNUll(Str) Then StrType=0 Exit Function End If If Trim(Str)="" Then StrType=0 Exit Function End If For I=1 To Len(Str) A=Asc(Mid(Str,I,1)) If A>=48 And A<=57 Then N=True If (A>=65 And A<=90) Or (A>=97 And A<=122) Or (A>=38 And A<=39) Then E=True If A<0 Then C=True Next If N And (Not E) And (Not C) Then StrType=1 Elseif E And (Not N) And (Not C) Then StrType=2 Elseif C And (Not E) And (Not N) Then StrType=3 Elseif C And E And (Not N) Then StrType=4 Elseif N And E And (Not C) Then StrType=5 Elseif C And N And (Not E) Then StrType=6 Elseif N And E And C Then StrType=7 Else StrType=0 End If If Err.Number<>0 Then Err.Clear End Function (出處:Viphot)
|