How to simulate C # char.IsLetter in VB6
I am not very happy with MSDN about the char.IsLetter description. I cannot understand that well. From common sense, Letter: AZ, az, Unicode string ... From what I'm testing, "~! @ # $% ^ ..." and 0-9 are not in the IsLetter category. Yes, the range is huge, How to simulate a C # char.IsLetter (write similar code) in VB6?
+3
source to share
5 answers
Option Explicit Private Declare Function IsCharAlphaW Lib "user32" (ByVal cChar As Integer) As Long Private Declare Function IsCharAlphaNumericW Lib "user32" (ByVal cChar As Integer) As Long Public Property Get IsAlpha(character As String) As Boolean IsAlpha = IsCharAlphaW(AscW(character)) End Property Public Property Get IsAlphaNumeric(character As String) As Boolean IsAlphaNumeric = IsCharAlphaNumericW(AscW(character)) End Property Public Property Get IsNumeric(character As String) As Boolean IsNumeric = IsAlphaNumeric(character) And Not IsAlpha(character) End Property Private Sub Form_Load() Debug.Print "a", IsAlpha("a") Debug.Print "Z", IsAlpha("Z") Debug.Print "0", IsAlpha("0"), IsNumeric("0") Debug.Print "ChrW$(&HFF21)", IsAlpha(ChrW$(&HFF21)) Debug.Print "ChrW$(&HFF10)", IsAlpha(ChrW$(&HFF10)), IsNumeric(ChrW$(&HFF10)) End Sub
+1
source to share
This works for me, but I did a very quick test. It uses the User32.dll IsCharAlphaW API , which should be available on Windows 2000 Professional and above. Try it and if it doesn't, I'll delete the answer so it doesn't confuse anyone.
Note: There is no error handling in the example.
Private Declare Function IsCharAlpha Lib "user32" Alias "IsCharAlphaW" (ByVal cChar As Long) As Long
Private Sub Command1_Click()
MsgBox "IsLetter(" & Text1.Text & ") = " & IsLetter(Left$(Text1.Text, 1))
End Sub
Private Function IsLetter(ByVal vCharacter As String) As Boolean
IsLetter = IsCharAlpha(AscW(vCharacter))
End Function
+1
source to share
Option Explicit Private Declare Function IsCharAlphaW Lib "user32" (ByVal cChar As Integer) As Long Private Declare Function IsCharAlphaNumericW Lib "user32" (ByVal cChar As Integer) As Long Public Property Get IsAlpha(character As String) As Boolean IsAlpha = IsCharAlphaW(AscW(character)) End Property Public Property Get IsAlphaNumeric(character As String) As Boolean IsAlphaNumeric = IsCharAlphaNumericW(AscW(character)) End Property Public Property Get IsNumeric(character As String) As Boolean IsNumeric = IsAlphaNumeric(character) And Not IsAlpha(character) End Property Private Sub Form_Load() Debug.Print "a", IsAlpha("a") Debug.Print "Z", IsAlpha("Z") Debug.Print "0", IsAlpha("0"), IsNumeric("0") Debug.Print "ChrW$(&HFF21)", IsAlpha(ChrW$(&HFF21)) Debug.Print "ChrW$(&HFF10)", IsAlpha(ChrW$(&HFF10)), IsNumeric(ChrW$(&HFF10)) End Sub
0
source to share
This tells you which character is the Unicode standard.
Public Const CT_CTYPE1 = &H1 ' ctype 1 information
Public Const CT_CTYPE2 = &H2 ' ctype 2 information
Public Const CT_CTYPE3 = &H4 ' ctype 3 information
Public Const C1_ALPHA = &H100 ' any letter
Public Const C1_BLANK = &H40 ' blank characters
Public Const C1_CNTRL = &H20 ' control characters
Public Const C1_DIGIT = &H4 ' decimal digits
Public Const C1_LOWER = &H2 ' lower case
Public Const C1_PUNCT = &H10 ' punctuation characters
Public Const C1_SPACE = &H8 ' spacing characters
Public Const C1_TRANSPARENT = &H1 ' new raster cap
Public Const C1_UPPER = &H1 ' upper case
Public Const C1_XDIGIT = &H80 ' hex digits
Public Const C1_DEFINED = &H200 ' Other defined char
Public Const C2_ARABICNUMBER = &H6 ' Arabic number
Public Const C2_BLOCKSEPARATOR = &H8 ' block separator
Public Const C2_COMMONSEPARATOR = &H7 ' common numeric separator
Public Const C2_EUROPENUMBER = &H3 ' European number, digit
Public Const C2_EUROPESEPARATOR = &H4 ' European numeric separator
Public Const C2_EUROPETERMINATOR = &H5 ' European numeric terminator
Public Const C2_LEFTTORIGHT = &H1 ' left to right
Public Const C2_NOTAPPLICABLE = &H0 ' no implicit directionality
Public Const C2_OTHERNEUTRAL = &HB ' other neutrals
Public Const C2_RIGHTTOLEFT = &H2 ' right to left
Public Const C2_WHITESPACE = &HA ' white space
Public Const C2_SEGMENTSEPARATOR = &H9 ' segment separator
Public Const C3_DIACRITIC = &H2 ' diacritic mark
Public Const C3_NONSPACING = &H1 ' nonspacing character
Public Const C3_NOTAPPLICABLE = &H0 ' ctype 3 is not applicable
Public Const C3_SYMBOL = &H8 ' symbols
Public Const C3_VOWELMARK = &H4 ' vowel mark
Function GetC1Type(B As String) As String
Dim Ret As Long
Dim A() As Integer
ReDim A(1)
A(0) = 0
A(1) = 0
Ret = GetStringTypeEx(&HB, CT_CTYPE1, B, -1, A(0))
Dim Alpha As Long
Dim Blank As Long
Dim Cntrl As Long
Dim Digit As Long
Dim LowerC As Long
Dim UpperC As Long
Dim Punct As Long
Dim SpaceChar As Long
Dim HexDigit As Long
Dim DefinedChar As Long
Dim CharTypeString
Dim X As Long
Alpha = A(0) And C1_ALPHA
Blank = A(0) And C1_BLANK
Cntrl = A(0) And C1_CNTRL
Digit = A(0) And C1_DIGIT
LowerC = A(0) And C1_LOWER
UpperC = A(0) And C1_UPPER
Punct = A(0) And C1_PUNCT
SpaceChar = A(0) And C1_SPACE
HexDigit = A(0) And C1_XDIGIT
DefinedChar = A(0) And C1_DEFINED
If Alpha = C1_ALPHA Then CharTypeString = CharTypeString & "Alpha "
If Blank = C1_BLANK Then CharTypeString = CharTypeString & "Blank "
If Cntrl = C1_CNTRL Then CharTypeString = CharTypeString & "Control "
If Digit = C1_DIGIT Then CharTypeString = CharTypeString & "Number "
If LowerC = C1_LOWER Then CharTypeString = CharTypeString & "Lower "
If UpperC = C1_UPPER Then CharTypeString = CharTypeString & "Upper "
If Punct = C1_PUNCT Then CharTypeString = CharTypeString & "Punct "
If SpaceChar = C1_SPACE Then CharTypeString = CharTypeString & "Space "
If Len(CharTypeString) = 0 Then
If DefinedChar = C1_DEFINED Then CharTypeString = CharTypeString & "Other_Defined "
If Len(CharTypeString) = 0 Then CharTypeString = CharTypeString & "Not_Defined "
End If
GetC1Type = CharTypeString
End Function
0
source to share