Question

Need a function that takes a character as a parameter and returns true if it is a letter.

Was it helpful?

Solution

Seanyboy's IsCharAlphaA answer is close. The best method is to use the W version like so:

Private Declare Function IsCharAlphaW Lib "user32" (ByVal cChar As Integer) As Long
Public Property Get IsLetter(character As String) As Boolean
    IsLetter = IsCharAlphaW(AscW(character))
End Property

Of course, this all rarely matters as all of VB6's controls are ANSI only

OTHER TIPS

This was part of the code posted by rpetrich in response to a question by Joel Spolsky. I felt it needed a post specific to the problem it solves. It really is brilliant.

Private Function IsLetter(ByVal character As String) As Boolean
    IsLetter = UCase$(character) <> LCase$(character)
End Function

You may be thinking to yourself, "Will this always work?" The documentation on the UCase and LCase functions, confirms that it will:

UCase Function Only lowercase letters are converted to uppercase; all uppercase letters and nonletter characters remain unchanged.

LCase Function Only uppercase letters are converted to lowercase; all lowercase letters and nonletter characters remain unchanged.

Private Function IsLetter(Char As String) As Boolean
    IsLetter = UCase(Char) Like "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]"
End Function

What's wrong with the following, which doesn't rely on obscure language behaviour?

Private Function IsLetter(ByVal ch As String) As Boolean
    IsLetter = (ch >= "A" and ch <= "Z") or (ch >= "a" and ch <= "z")
End Function

I believe we can improve upon this a little more. rpetrich's code will work, but perhaps only by luck. The API call's parameter should be a TCHAR (WCHAR here actually) and not a Long. This also means no fiddling with converting to a Long or masking with &HFFFF. This by the way is Integer and adds an implicit conversion to Long here too. Perhaps he meant &HFFFF& in this case?

On top of that it might be best to explictly call the UnicoWS wrapper for this API call, for Win9X compatibility. The UnicoWS.dll may need to be deployed but at least we gain that option. Then again maybe from VB6 this is automagically redirected, I don't have Win9X installed to test it.

Option Explicit

Private Declare Function IsCharAlphaW Lib "unicows" (ByVal WChar As Integer) As Long

Private Function IsLetter(Character As String) As Boolean
    IsLetter = IsCharAlphaW(AscW(Character))
End Function

Private Sub Main()
    MsgBox IsLetter("^")
    MsgBox IsLetter("A")
    MsgBox IsLetter(ChrW$(&H34F))
    MsgBox IsLetter(ChrW$(&HFEF0))
    MsgBox IsLetter(ChrW$(&HFEFC))
End Sub

Looking around a bit came up with the following...

Private Declare Function IsCharAlphaA Lib "user32" Alias "IsCharAlphaA" (ByVal cChar As Byte) As Long

I believe IsCharAlphaA tests ANSI character sets and IsCharAlpha tests ASCII. I may be wrong.

Private Function IsAlpha(ByVal vChar As String) As Boolean
  Const letters$ = "abcdefghijklmnopqrstuvwxyz"

  If InStr(1, letters, LCase$(vChar)) > 0 Then IsAlpha = True
End Function

I use this in VBA

Function IsLettersOnly(Value As String) As Boolean
   IsLettersOnly = Len(Value) > 0 And Not UCase(Value) Like "*[!A-Z]*"
End Function

It doesn't exactly document itself. And it may be slow. It's a clever hack, but that's all it is. I'd be tempted to be more obvious in my checking. Either use regex's or write a more obvious test.

public bool IsAlpha(String strToCheck)
{
    Regex objAlphaPattern=new Regex("[^a-zA-Z]");
    return !objAlphaPattern.IsMatch(strToCheck);
}

public bool IsCharAlpha(char chToCheck)
{
    return ((chToCheck=>'a') and (chToCheck<='z')) or ((chToCheck=>'A') and (chToCheck<='Z'))
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top