一聚教程网:一个值得你收藏的教程网站

热门教程

自定义的字符串类型检测函数

时间:2022-06-30 10:39:03 编辑:袖梨 来源:一聚教程网

程序需要,做了一个自定义的检测函数,主要检测字符串的类型,因为需求,将'和%也一同放入英文字符类型中。原理很简单,将字符拆开单取获得ASC码,在什么范围就为什么类型,当然检测有局限性。
代码如下: '#############################################################
'名称:StrType(Str)
'用途:判断字符串类型
'返回0-空
'返回1-数字
'返回2-英文
'返回3-汉字
'返回4-英汉
'返回5-英数
'返回6-汉数
'返回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

热门栏目